-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAddTwoNumbers.java
158 lines (144 loc) · 4.25 KB
/
AddTwoNumbers.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package linkedlist;
// Source : https://leetcode.com/problems/add-two-numbers/
// Id : 2
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-03
// Topic : Linked list
// Level : Medium
// Other :
// Tips :
// Links :
// Result : 94.51% 90.73%
public class AddTwoNumbers {
// 2 ms
public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
// public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode sentinel = new ListNode(-1);
ListNode cur = sentinel, pre = null;
while (l1 != null && l2 != null) {
int tmp = l1.val + l2.val + carry;
carry = tmp > 9 ? 1 : 0;
cur.next = new ListNode(tmp % 10);
cur = cur.next;
l1 = l1.next;
l2 = l2.next;
}
if (l1 == null)
cur.next = l2;
else
cur.next = l1;
pre = cur;
cur = cur.next;
while (carry == 1 && cur != null) {
if (cur.val == 9) {
cur.val = 0;
carry = 1;
pre = cur;
cur = cur.next;
} else {
cur.val = cur.val + 1;
carry = 0;
}
}
if (carry == 1)
pre.next = new ListNode(1);
return sentinel.next;
}
// 2ms
public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
// public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode();
ListNode cur = result, pre = new ListNode();
boolean carry = false;
while (l1 != null || l2 != null || carry) {
int val = 0;
if (l1 != null) {
val += l1.val;
l1 = l1.next;
}
if (l2 != null) {
val += l2.val;
l2 = l2.next;
}
if (carry) {
val++;
carry = false;
}
if (val > 9) {
val = val % 10;
carry = true;
}
cur.val = val;
pre = cur;
ListNode next = new ListNode();
cur.next = next;
cur = cur.next;
}
if (pre != null) {
pre.next = null;
}
return result;
}
// practice
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode sentinel = new ListNode(), cur = sentinel;
int carry = 0;
while (true) {
if (l1 == null || l2 == null) {
if (carry == 0) {
if (l1 == null) {
cur.next = l2;
break;
}
if (l2 == null) {
cur.next = l1;
break;
}
} else {
if (l1 == null && l2 == null) {
cur.next = new ListNode(1);
break;
} else {
ListNode tmp = l1 == null ? l2 : l1; // l1 or l2
int val = tmp.val + carry;
if (val > 9) {
carry = 1;
val = val % 10;
} else {
carry = 0;
}
cur.next = new ListNode(val);
cur = cur.next;
if (tmp == l1) {
l1 = l1.next;
} else {
l2 = l2.next;
}
}
}
} else {
int val = l1.val + l2.val + carry;
if (val > 9) {
carry = 1;
val = val % 10;
} else {
carry = 0;
}
cur.next = new ListNode(val);
cur = cur.next;
l1 = l1.next;
l2 = l2.next;
}
}
return sentinel.next;
}
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int x) {
val = x;
}
}
}