-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
178 lines (153 loc) · 3.72 KB
/
Copy pathAddTwoNumbers.java
File metadata and controls
178 lines (153 loc) · 3.72 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
public class AddTwoNumbers {
public static void main(String[] args)
{
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8
AddTwoNumbers a = new AddTwoNumbers();
ListNode l1 = new ListNode(9);
l1.next = new ListNode(9);
l1.next.next = new ListNode(9);
l1.next.next.next = new ListNode(9);
l1.next.next.next.next = new ListNode(9);
l1.next.next.next.next.next = new ListNode(9);
// l1.next.next.next.next.next.next = new ListNode(0);
// l1.next.next.next.next.next.next.next = new ListNode(1);
// l1.next.next.next.next.next.next.next.next = new ListNode(6);
// l1.next.next.next.next.next.next.next.next.next = new ListNode(1);
ListNode l2 = new ListNode(1);
// l2.next = new ListNode(5);
// l2.next.next = new ListNode(8);
// l2.next.next.next = new ListNode(6);
// l2.next.next.next.next = new ListNode(2);
// l2.next.next.next.next.next = new ListNode(5);
// l2.next.next.next.next.next.next = new ListNode(8);
// l2.next.next.next.next.next.next.next = new ListNode(2);
// l2.next.next.next.next.next.next.next.next = new ListNode(6);
// l2.next.next.next.next.next.next.next.next.next = new ListNode(1);
print(a.addTwoNumbers(l1, l2));
}
private static void print(ListNode a) {
System.out.println("NUMBER IS REVERSED");
while(a != null)
{
System.out.print(a.val);
a = a.next;
}
}
// template
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// begining and end of the list
ListNode head = null, listEnd = null;
// flag to see if there's only one item
boolean first = true;
// carry digit
int carry = 0;
// while the two lists are the same lengths
while(l1 != null && l2 != null)
{
// calculate answer
int ans = l1.val + l2.val + carry;
// reset the carry digit
carry = 0;
// calculate to see if there's a new carry
if(ans >= 10)
{
carry = 1;
ans = ans % 10;
}
// see if it's the first node being created
if(first)
{
listEnd = new ListNode(ans);
head = listEnd;
first = false;
}
else // just add it to the list
{
listEnd.next = new ListNode(ans);
listEnd = listEnd.next;
}
// if there's a carry and there is no more digits to add
if(carry == 1 && l1.next == null && l2.next == null)
{
listEnd.next = new ListNode(1);
return head;
}
l1 = l1.next;
l2 = l2.next;
}
// while l1 is not empty and l2 is empty
if(l1 != null)
{
// if l1 is the only list
if(head == null)
head = l1;
else
{
ListNode temp = l1;
// iterate through the list
while(temp != null)
{
// calculate answer
int ans = temp.val + carry;
// calculate carry
if(ans >= 10)
{
carry = 1;
ans = ans % 10;
}
else
carry = 0;
// add to list
listEnd.next = new ListNode(ans);
listEnd = listEnd.next;
temp = temp.next;
}
// additional carry at the end
if(carry == 1 && listEnd.next == null)
{
listEnd.next = new ListNode(1);
return head;
}
}
}
// repeat of the above, would be nice to put into it's own function but you know
if(l2 != null)
{
if(head == null)
head = l2;
else
{
ListNode temp = l2;
while(temp != null)
{
int ans = temp.val + carry;
if(ans >= 10)
{
carry = 1;
ans = ans % 10;
}
else
carry = 0;
listEnd.next = new ListNode(ans);
listEnd = listEnd.next;
temp = temp.next;
}
if(carry == 1 && listEnd.next == null)
{
listEnd.next = new ListNode(1);
return head;
}
}
}
return head;
}
}