-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0167. Add Two Numbers.java
More file actions
37 lines (37 loc) · 1001 Bytes
/
0167. Add Two Numbers.java
File metadata and controls
37 lines (37 loc) · 1001 Bytes
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
34
35
36
37
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode a, ListNode b) {
ListNode result = new ListNode(0);
ListNode curr = result;
if (a == null && b == null) {
return result;
}
int carry = 0;
int sum = 0;
while (a != null || b != null) {
if (a == null) {
sum = b.val + carry;
b = b.next; //only move b
} else if (b == null) {
sum = a.val + carry;
a = a.next;
} else {
sum = a.val + b.val + carry; //carry is the overflow from the addition one unit before
a = a.next;
b = b.next;
}
carry = sum / 10;
sum %= 10; //part that doesn't overflow
curr.next = new ListNode(sum);
curr = curr.next;
}
if (carry > 0) { //loop ignores this last overflowing value
curr.next = new ListNode(carry);
}
return result.next;
}
}