-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2.java
More file actions
51 lines (42 loc) · 1.2 KB
/
Copy pathLC2.java
File metadata and controls
51 lines (42 loc) · 1.2 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ListNode {
int val;
ListNode next;
ListNode() {};
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
enum Carry {
ZERO(0),
ONE(1);
private final int value;
Carry(int value) {
this.value = value;
}
private int getValue() {
return value;
}
}
ListNode anchor = new ListNode(0);
ListNode iterator = anchor;
Carry carryBit = Carry.ZERO;
while(l1 != null || l2 != null) {
int x = (l1 != null) ? l1.val : 0;
int y = (l2 != null) ? l2.val : 0;
int sum = x + y + carryBit.getValue();
carryBit = (sum > 9) ? Carry.ONE : Carry.ZERO;
sum = sum % 10;
iterator.next = new ListNode(sum);
iterator = iterator.next;
if (l1.next != null) l1 = l1.next;
if (l2.next != null) l2 = l2.next;
}
return anchor.next;
}
}