题目描述
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
1 | Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) |
这里需要对两个链表进行加法,首先需要将列表进行反转,然后从表头进行链表的遍历。然后对两个链表进行同时从头到尾遍历。每一次遍历后将值相加,然后保存到一个新的节点中,然后两个节点都向后移一位。最后要注意两个表都遍历到头了以后,如果carry为1,还需要额外生成一个节点连接到结果的最后。
在返回前,需要对结果再进行一次反转。
1 | class Solution: |