原题目
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
思路
- 思路一
让两个链表的对应位置求和,有进位时进位置 1。
问题:用链表的 next 指针判断时最后的进位无法处理
- 思路二
- 遍历链表,计算每个链表表示的数值,然后将两个数值相加得到 ans
- 若 ans 为 0 ,直接返回节点值为 0 的节点(链表);若 ans 不为 0,对 ans 逆序得到链表
第一遍解法
# Runtime: 80 ms, faster than 92.37% of Python3
# Memory Usage: 13.2 MB, less than 5.21% of Python3
class Solution:
def addTwoNumbers(self, l1, l2):
num1 = 0
num2 = 0
base1 = 0
p = l1
while p != None:
num1 = num1 + p.val * 10 ** base1
p = p.next
base1 = base1 + 1
base2 = 0
p = l2
while p != None:
num2 = num2 + p.val * 10 ** base2
p = p.next
base2 = base2 + 1
ans = num1 + num2
l3 = ListNode(0)
if ans == 0:
return l3
p = l3
while ans != 0:
q = ListNode(0)
q.val = ans % 10
p.next = q
ans = ans // 10
p = p.next
l3 = l3.next
return l3
网上好的解法
# 两个链表中任一不为空就求和
# sum 既是求和数又是进位数
# 最后判定是否加上进位之后超过原来最长链表长度
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode sentinel = new ListNode(0);
ListNode d = sentinel;
int sum = 0;
while (c1 != null || c2 != null) {
sum /= 10;
if (c1 != null) {
sum += c1.val;
c1 = c1.next;
}
if (c2 != null) {
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum % 10);
d = d.next;
}
if (sum / 10 == 1)
d.next = new ListNode(1);
return sentinel.next;
}
}
# 三目运算符用到极致
# extra 在循环中不用后面的 if 判断
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(0), *p = &preHead;
int extra = 0;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;
extra = sum / 10;
p->next = new ListNode(sum % 10);
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
自己可以改进的地方
- 将从两个链表中提取对应数字优化为加法的方法
最简代码
class Solution:
def addTwoNumbers(self, l1, l2):
root = p = ListNode(0)
carry = 0
while l1 or l2 or carry:
v1 = 0
v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, sum = divmod(v1 + v2 + carry, 10)
p.next = ListNode(sum)
p = p.next
return root.next
获得的思考
逆序链表求和,原理和手写计算求和完全相同,从低位加到高位,还要注意进位问题。
代码实现上,链表需要插入节点,因此需要一个(无用的)头节点才能在后面插入节点,同时应该考虑到较长的链表最后一个节点加上进位后是否还会进位。如果还会进位的话需要再加一个节点才行。
while l1 or l2 or carry
既考虑了两个链表长度问题,又考虑了较长链表进位问题,是代码的精髓。
pythondivmod()
函数用于便捷计算数学取余,用到的时候要想得起来。
2, 1 = divmod(5, 2)