Skip to content

Commit 954a408

Browse files
committed
Update solution 002
1 parent 8264c84 commit 954a408

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Complete solutions to Leetcode problems, updated daily.
2727

2828
| # | Title | Tags |
2929
|---|---|---|
30+
| 002 | [Add Two Numbers](https://github.com/yanglbme/leetcode/tree/master/solution/002.Add%20Two%20Numbers) | `Linked List`, `Math` |
3031
| 019 | [Remove Nth Node From End of List](https://github.com/yanglbme/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
3132
| 024 | [Swap Nodes in Pairs](https://github.com/yanglbme/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
3233
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## 两数相加
2+
### 题目描述
3+
4+
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
5+
6+
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
7+
8+
示例:
9+
```
10+
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
11+
输出:7 -> 0 -> 8
12+
原因:342 + 465 = 807
13+
```
14+
15+
### 解法
16+
同时遍历两个链表,对应值相加(还有 quotient)求余数得到值并赋给新创建的结点。而商则用quotient存储,供下次相加。
17+
18+
初始版本:
19+
20+
```java
21+
/**
22+
* Definition for singly-linked list.
23+
* public class ListNode {
24+
* int val;
25+
* ListNode next;
26+
* ListNode(int x) { val = x; }
27+
* }
28+
*/
29+
class Solution {
30+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
31+
ListNode res = new ListNode(-1);
32+
ListNode cur = res;
33+
int quotient = 0;
34+
int t = 0;
35+
while (l1 != null && l2 != null) {
36+
t = l1.val + l2.val + quotient;
37+
quotient = t / 10;
38+
ListNode node = new ListNode(t % 10);
39+
cur.next = node;
40+
l1 = l1.next;
41+
l2 = l2.next;
42+
cur = node;
43+
}
44+
45+
while (l1 != null) {
46+
t = l1.val + quotient;
47+
quotient = t / 10;
48+
ListNode node = new ListNode(t % 10);
49+
cur.next = node;
50+
l1 = l1.next;
51+
cur = node;
52+
}
53+
54+
while (l2 != null) {
55+
t = l2.val + quotient;
56+
quotient = t / 10;
57+
ListNode node = new ListNode(t % 10);
58+
cur.next = node;
59+
l2 = l2.next;
60+
cur = node;
61+
}
62+
63+
if (quotient != 0) {
64+
cur.next = new ListNode(quotient);
65+
cur = cur.next;
66+
}
67+
68+
return res.next;
69+
70+
}
71+
}
72+
```
73+
74+
简化版本:
75+
```java
76+
/**
77+
* Definition for singly-linked list.
78+
* public class ListNode {
79+
* int val;
80+
* ListNode next;
81+
* ListNode(int x) { val = x; }
82+
* }
83+
*/
84+
class Solution {
85+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
86+
ListNode res = new ListNode(-1);
87+
ListNode cur = res;
88+
int quotient = 0;
89+
while (l1 != null || l2 != null || quotient != 0) {
90+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + quotient;
91+
quotient = t / 10;
92+
ListNode node = new ListNode(t % 10);
93+
cur.next = node;
94+
cur = node;
95+
l1 = (l1 == null) ? l1 : l1.next;
96+
l2 = (l2 == null) ? l2 : l2.next;
97+
}
98+
return res.next;
99+
}
100+
}
101+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3+
ListNode res = new ListNode(-1);
4+
ListNode cur = res;
5+
int quotient = 0;
6+
while (l1 != null || l2 != null || quotient != 0) {
7+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + quotient;
8+
quotient = t / 10;
9+
ListNode node = new ListNode(t % 10);
10+
cur.next = node;
11+
cur = node;
12+
l1 = (l1 == null) ? l1 : l1.next;
13+
l2 = (l2 == null) ? l2 : l2.next;
14+
}
15+
return res.next;
16+
}
17+
}

0 commit comments

Comments
 (0)