-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
95 lines (80 loc) · 2.63 KB
/
Solution.java
File metadata and controls
95 lines (80 loc) · 2.63 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
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
package addtwonumbers;
/**
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
* <p>
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
* <p>
* Example:
* <p>
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*/
// Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0, currSum = 0, sum;
boolean isHeadSet = false;
ListNode result = null, current = null;
// TODO: Refactor this while to loop as long as either list has a node - remove second if/while
while (l1 != null && l2 != null) {
sum = carry + l1.val + l2.val;
if (sum > 9) {
carry = sum / 10;
currSum = sum % 10;
} else {
carry = 0;
currSum = sum;
}
if (!isHeadSet) {
current = new ListNode(currSum);
result = current;
isHeadSet = true;
} else {
current.next = new ListNode(currSum);
current = current.next;
}
l1 = l1.next;
l2 = l2.next;
}
// check if either l1 or l2 still has nodes left
// [1], [9,9] -> [0, 0, 1]
if (l1 != null || l2 != null) {
ListNode rem = l1 != null ? l1 : l2;
while (rem != null || carry > 0) {
if (rem == null) {
current.next = new ListNode(carry);
current = current.next;
carry = 0;
break;
} else {
sum = carry + rem.val;
if (sum > 9) {
carry = sum / 10;
currSum = sum % 10;
//carry = 0;
} else {
currSum = sum;
carry = 0;
}
current.next = new ListNode(currSum);
current = current.next;
rem = rem.next;
}
}
}
// check if carry still has a value
if (carry > 0) {
current.next = new ListNode(carry);
current = current.next;
}
return result;
}
}