-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTwoNumbers.java
111 lines (92 loc) · 2.6 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
import java.util.List;
public class AddTwoNumbers {
public static ListNode solution(ListNode l1, ListNode l2) {
int carry = 0, tmp = 0;
ListNode head = new ListNode(0), cur = head;
while (true) {
if (l1.val + l2.val + carry >= 10) {
tmp = (l1.val + l2.val + carry) - 10;
carry = 1;
} else {
tmp = l1.val + l2.val + carry;
carry = 0;
}
l1 = l1.next;
l2 = l2.next;
cur.val = tmp;
if (l1 == null || l2 == null) {
cur.next = null;
break;
} else {
cur.next = new ListNode(0);
cur = cur.next;
}
}
while (l1 != null) {
if ((l1.val + carry) == 10) {
l1.val = 0;
cur.next = new ListNode(0);
cur = cur.next;
cur.val = l1.val;
carry = 1;
} else {
l1.val += carry;
cur.next = l1;
carry = 0;
break;
}
l1 = l1.next;
}
while (l2 != null) {
if ((l2.val + carry) == 10) {
l2.val = 0;
cur.next = new ListNode(0);
cur = cur.next;
cur.val = l2.val;
carry = 1;
} else {
l2.val += carry;
cur.next = l2;
carry = 0;
break;
}
l2 = l2.next;
}
if (carry != 0) {
cur.next = new ListNode(1);
}
return head;
}
public static void printList(ListNode node) {
while (node != null) {
System.out.println(node.val);
node = node.next;
}
System.out.println();
}
public static void main(String[] args){
ListNode node;
ListNode l1 = new ListNode(9);
l1.next = new ListNode(9);
// l1.next.next = new ListNode(3);
ListNode l2 = new ListNode(9);
// l2.next = new ListNode(6);
// l2.next.next = new ListNode(4);
// l2.next.next.next = new ListNode(5);
// l2.next.next.next.next = new ListNode(5);
node = solution(l1, l2);
System.out.println("output: ");
printList(node);
}
}
class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
public ListNode getNext() {
return next;
}
public boolean hasNext() {
return this.next != null;
}
}