002. Add Two Numbers

  • 26.7%

https://leetcode.com/problems/add-two-numbers/

You are given two linked lists representing two non-negative numbers. 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.

1
2
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

方法一:

我的代码实现一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
else if(!l2) return l1;
int carry = 0;
ListNode* dummy = new ListNode(-1);
ListNode* cur = dummy;
while(l1 || l2 || carry){
carry += l1 ? l1->val : 0;
carry += l2 ? l2->val : 0;
cur->next = new ListNode(carry%10);
cur = cur->next;
carry /= 10;
l1 = l1 ? l1->next : 0;
l2 = l2 ? l2->next : 0;
}
return dummy->next;
}
};

我的代码实现二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(-1);
int carry = 0;
ListNode* cur = dummy;
while(l1 || l2 || carry){
carry += l1 ? l1->val : 0;
carry += l2 ? l2->val : 0;
cur->next = new ListNode(carry%10);
carry /= 10;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
cur = cur->next;
}
return dummy->next;
}
};

40ms, 24.98%, 17 July 2016

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
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;
}
};

https://discuss.leetcode.com/topic/5905/c-sharing-my-11-line-c-solution-can-someone-make-it-even-more-concise/4

How about this?

1
2
3
4
5
6
7
8
9
10
11
12
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode preHead(0), *p = &preHead;
int extra = 0;
while (l1 || l2 || extra) {
if (l1) extra += l1->val, l1 = l1->next;
if (l2) extra += l2->val, l2 = l2->next;
p->next = new ListNode(extra % 10);
extra /= 10;
p = p->next;
}
return preHead.next;
}

python


https://discuss.leetcode.com/topic/21192/python-concise-solution

188ms, 7.15%, 17 July 2016

Python concise solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy = cur = ListNode(0)
carry = 0
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
cur.next = ListNode(carry%10)
cur = cur.next
carry //= 10
return dummy.next

https://discuss.leetcode.com/topic/8909/clear-python-code-straight-forward

172ms, 15.49%, 17 July 2016

Clear python code, straight forward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
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

https://discuss.leetcode.com/topic/8909/clear-python-code-straight-forward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

https://discuss.leetcode.com/topic/14575/python-for-the-win

Python for the win

Python supports arbitrarily large integers, so I can safely turn the two lists into ints, add them, and turn the sum into a list.

1
2
3
4
5
6
7
8
9
10
class Solution:
def addTwoNumbers(self, l1, l2):
def toint(node):
return node.val + 10 * toint(node.next) if node else 0
def tolist(n):
node = ListNode(n % 10)
if n > 9:
node.next = tolist(n / 10)
return node
return tolist(toint(l1) + toint(l2))

Iterative tolist instead of recursive:

1
2
3
4
5
6
7
8
9
10
class Solution:
def addTwoNumbers(self, l1, l2):
def toint(node):
return node.val + 10 * toint(node.next) if node else 0
n = toint(l1) + toint(l2)
first = last = ListNode(n % 10)
while n > 9:
n /= 10
last.next = last = ListNode(n % 10)
return first

And a very different solution that could sum arbitrarily many addends, not just two:

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def addTwoNumbers(self, l1, l2):
addends = l1, l2
dummy = end = ListNode(0)
carry = 0
while addends or carry:
carry += sum(a.val for a in addends)
addends = [a.next for a in addends if a.next]
end.next = end = ListNode(carry % 10)
carry /= 10
return dummy.next

java


https://leetcode.com/articles/add-two-numbers/

Time complexity : O(\max(m, n))O(max(m,n)). Assume that mm and nn represents the length of l1l1 and l2l2 respectively, the algorithm above iterates at most \max(m, n)max(m,n) times.

Space complexity : O(\max(m, n))O(max(m,n)). The length of the new list is at most \max(m,n) + 1max(m,n)+1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
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;
}
}

https://discuss.leetcode.com/topic/799/is-this-algorithm-optimal-or-what

Is this Algorithm optimal or what?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
}
}

https://discuss.leetcode.com/topic/6220/my-accepted-java-solution

My accepted Java solution

Two things to make the code simple:

  1. Whenever one of the two ListNode is null, replace it with 0.
  2. Keep the while loop going when at least one of the three conditions is met.

Let me know if there is something wrong. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode prev = new ListNode(0);
ListNode head = prev;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
ListNode cur = new ListNode(0);
int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
cur.val = sum % 10;
carry = sum / 10;
prev.next = cur;
prev = cur;

l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next;
}
return head.next;
}
}

https://discuss.leetcode.com/topic/39130/4ms-11lines-java-solution

4ms 11lines java solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ln1 = l1, ln2 = l2, head = null, node = null;
int carry = 0, remainder = 0, sum = 0;
head = node = new ListNode(0);

while(ln1 != null || ln2 != null || carry != 0) {
sum = (ln1 != null ? ln1.val : 0) + (ln2 != null ? ln2.val : 0) + carry;
carry = sum / 10;
remainder = sum % 10;
node = node.next = new ListNode(remainder);
ln1 = (ln1 != null ? ln1.next : null);
ln2 = (ln2 != null ? ln2.next : null);
}
return head.next;
}
}

https://discuss.leetcode.com/topic/44133/java-concise-solution

Java concise solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode p, dummy = new ListNode(0);
p = dummy;
while (l1 != null || l2 != null || carry != 0) {
if (l1 != null) {
carry += l1.val;
l1 = l1.next;
}
if (l2 != null) {
carry += l2.val;
l2 = l2.next;
}
p.next = new ListNode(carry%10);
carry /= 10;
p = p.next;
}
return dummy.next;
}

谢谢你,可爱的朋友。