-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddTwoNumbers.java
69 lines (56 loc) · 2.05 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
package AddTwoNumbers;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode a = l1; //to the head of the 1st list
ListNode b = l2; //to the head of the 2nd list
ListNode list_head = new ListNode(0); //"new" the head of our list
ListNode list_current = list_head; //"current" to the head of our list
int overload =0; //carry
while(a != null || b != null){ //any one is not null
int a_digit;
int b_digit;
int sum;
if(a == null){ //if a is null
a_digit = 0;
}
else{
a_digit = a.val;
a = a.next;
}
if(b == null){ //if b is null
b_digit =0;
}
else{
b_digit = b.val;
b = b.next;
}
sum = a_digit + b_digit + overload; //note: also add overload(carry)
if(overload == 1)
overload =0;
if(sum >= 10){
sum = sum -10;
overload = 1;
}
list_current.val = sum; //the current value
if(a == null && b == null){}
else{
list_current.next = new ListNode(0); //"new" the next node
list_current = list_current.next; //"current" to the next node
}
}
//note: the special case, when a and b are both null, and there is an overload
if(overload ==1){
list_current.next = new ListNode(1); //"new" the next node with "1"
list_current = list_current.next; //"current" to the next node
}
return list_head; //note: return the "head" of our linked list
}
}