|
1 |
| -# [02.05. Sum Lists](https://leetcode-cn.com/problems/sum-lists-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.</p> |
5 |
| -
|
6 |
| -<p> </p> |
7 |
| -
|
8 |
| -<p><strong>Example: </strong></p> |
9 |
| -
|
10 |
| -<pre> |
11 |
| -<strong>Input: </strong>(7 -> 1 -> 6) + (5 -> 9 -> 2). That is, 617 + 295. |
12 |
| -<strong>Output: </strong>2 -> 1 -> 9. That is, 912. |
13 |
| -</pre> |
14 |
| -
|
15 |
| -<p><strong>Follow Up: </strong>Suppose the digits are stored in forward order. Repeat the above problem.</p> |
16 |
| -
|
17 |
| -<p><strong>Example: </strong></p> |
18 |
| -
|
19 |
| -<pre> |
20 |
| -<strong>Input: </strong>(6 -> 1 -> 7) + (2 -> 9 -> 5). That is, 617 + 295. |
21 |
| -<strong>Output: </strong>9 -> 1 -> 2. That is, 912. |
22 |
| -</pre> |
23 |
| - |
24 |
| - |
25 |
| - |
26 |
| -## Solutions |
27 |
| - |
28 |
| - |
29 |
| -### Python3 |
30 |
| - |
31 |
| -```python |
32 |
| - |
33 |
| -``` |
34 |
| - |
35 |
| -### Java |
36 |
| - |
37 |
| -```java |
38 |
| - |
39 |
| -``` |
40 |
| - |
41 |
| -### ... |
42 |
| -``` |
43 |
| - |
44 |
| -``` |
| 1 | +# [02.05. Sum Lists](https://leetcode-cn.com/problems/sum-lists-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p> </p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p><strong>Example: </strong></p> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +<pre> |
| 17 | + |
| 18 | +<strong>Input: </strong>(7 -> 1 -> 6) + (5 -> 9 -> 2). That is, 617 + 295. |
| 19 | + |
| 20 | +<strong>Output: </strong>2 -> 1 -> 9. That is, 912. |
| 21 | + |
| 22 | +</pre> |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +<p><strong>Follow Up: </strong>Suppose the digits are stored in forward order. Repeat the above problem.</p> |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +<p><strong>Example: </strong></p> |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +<pre> |
| 35 | + |
| 36 | +<strong>Input: </strong>(6 -> 1 -> 7) + (2 -> 9 -> 5). That is, 617 + 295. |
| 37 | + |
| 38 | +<strong>Output: </strong>9 -> 1 -> 2. That is, 912. |
| 39 | + |
| 40 | +</pre> |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Solutions |
| 46 | + |
| 47 | + |
| 48 | +### Python3 |
| 49 | + |
| 50 | +```python |
| 51 | +# Definition for singly-linked list. |
| 52 | +# class ListNode: |
| 53 | +# def __init__(self, x): |
| 54 | +# self.val = x |
| 55 | +# self.next = None |
| 56 | + |
| 57 | +class Solution: |
| 58 | + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: |
| 59 | + p = ListNode(-1) |
| 60 | + carry, t = 0, p |
| 61 | + while l1 or l2: |
| 62 | + s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry |
| 63 | + carry = 1 if s > 9 else 0 |
| 64 | + t.next = ListNode(s % 10) |
| 65 | + t = t.next |
| 66 | + l1 = l1.next if l1 else l1 |
| 67 | + l2 = l2.next if l2 else l2 |
| 68 | + t.next = None if carry == 0 else ListNode(carry) |
| 69 | + return p.next |
| 70 | + |
| 71 | + |
| 72 | +``` |
| 73 | + |
| 74 | +### Java |
| 75 | + |
| 76 | +```java |
| 77 | +/** |
| 78 | + * Definition for singly-linked list. |
| 79 | + * public class ListNode { |
| 80 | + * int val; |
| 81 | + * ListNode next; |
| 82 | + * ListNode(int x) { val = x; } |
| 83 | + * } |
| 84 | + */ |
| 85 | +class Solution { |
| 86 | + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { |
| 87 | + ListNode p = new ListNode(-1); |
| 88 | + int carry = 0; |
| 89 | + ListNode t = p; |
| 90 | + while (l1 != null || l2 != null) { |
| 91 | + int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; |
| 92 | + t.next = new ListNode(s % 10); |
| 93 | + carry = s > 9 ? 1 : 0; |
| 94 | + t = t.next; |
| 95 | + l1 = l1 == null ? l1 : l1.next; |
| 96 | + l2 = l2 == null ? l2 : l2.next; |
| 97 | + } |
| 98 | + t.next = carry == 0 ? null : new ListNode(carry); |
| 99 | + return p.next; |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### ... |
| 105 | +``` |
| 106 | +
|
| 107 | +``` |
0 commit comments