-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd_2.java
38 lines (38 loc) · 1.32 KB
/
Add_2.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
public class Add_2 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// My solution using helper and recursion
return helper(l1,l2,0);
/* Standard solution using iteration
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null || carry > 0) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
return dummyHead.next;
*/
}
private ListNode helper(ListNode l1, ListNode l2, int carry) {
if (l1 == null && l2 == null && carry == 0)
return null;
int val1 = l1==null ? 0 : l1.val;
int val2 = l2==null ? 0 : l2.val;
ListNode next1 = l1==null ? null : l1.next;
ListNode next2 = l2==null ? null : l2.next;
ListNode res = new ListNode((val1+val2+carry)%10);
res.next = helper(next1, next2, (val1+val2+carry)/10);
return res;
}
}