-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
59 lines (47 loc) · 1.62 KB
/
AddTwoNumbers.java
File metadata and controls
59 lines (47 loc) · 1.62 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
package LinkedLists;
public class AddTwoNumbers {
/**
* When the numbers are already reversed.
*/
public static Node addTwoNumbers(Node l1, Node l2) {
Node head = new Node(0);
Node l3 = head;
int carry = 0;
while (l1 != null || l2 != null) {
int l1_data = (l1 != null) ? l1.data : 0;
int l2_data = (l2 != null) ? l2.data : 0;
int currentSum = l1_data + l2_data + carry;
carry = currentSum / 10;
int lastDigit = currentSum % 10;
l3.next = new Node(lastDigit);
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
l3 = l3.next;
}
if (carry > 0) l3.next = new Node(carry);
return head.next;
}
/**
* The two lists have numbers in correct order.
*/
static Node addTwoLists(Node first, Node second) {
Node l1 = ReverseList.reverseList(first);
Node l2 = ReverseList.reverseList(second);
Node head = new Node(0);
Node l3 = head;
int carry = 0;
while (l1 != null || l2 != null) {
int l1_data = (l1 != null) ? l1.data : 0;
int l2_data = (l2 != null) ? l2.data : 0;
int currentSum = l1_data + l2_data + carry;
carry = currentSum / 10;
int lastDigit = currentSum % 10;
l3.next = new Node(lastDigit);
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
l3 = l3.next;
}
if (carry > 0) l3.next = new Node(carry);
return ReverseList.reverseList(head.next);
}
}