Repository navigation
Expand file tree
/
Copy pathAddTwoNumbersII.java
More file actions
158 lines (123 loc) · 3.99 KB
/
Copy pathAddTwoNumbersII.java
File metadata and controls
158 lines (123 loc) · 3.99 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
public class AddTwoNumbersII {
public static void main(String[] args) {
// 445. Add Two Numbers II
// https://leetcode.com/problems/add-two-numbers-ii/
ListNode head = new ListNode();
head.next = new ListNode(2);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(1);
ListNode head2 = new ListNode();
head2.next = new ListNode(2);
head2.next.next = new ListNode(2);
head2.next.next.next = new ListNode(1);
display(addTwoNumbers(head, head2));
}
public static void display(ListNode l1){
ListNode templl = l1.next ;
while (templl != null) {
System.out.print(templl.val+" ");
templl = templl.next ;
}
System.out.println("END");
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode reversel1 = reverse(l1); // 3 4 2 7
ListNode reversel2 = reverse(l2); // 4 6 5
ListNode temp1 = reversel1; // 3
ListNode temp2 = reversel2;// 4
ListNode dummy = new ListNode();
ListNode temp = dummy; // 0
int carry = 0;
while (temp1 != null && temp2 != null) {
int sum = temp1.val + temp2.val + carry;
if (sum >= 10) {
temp.val = sum % 10;
carry = sum / 10;
} else {
temp.val = sum;
carry = 0;
}
if (temp1.next != null && temp2.next != null) {
temp.next = new ListNode();
temp = temp.next;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
while (temp1 != null) {
int sum = temp1.val + carry;
// new node creation
temp.next = new ListNode();
temp = temp.next;
if (sum >= 10) {
// temp.next = new ListNode();
// temp = temp.next ; writing two times in if write it above to create node
temp.val = sum % 10;
carry = sum / 10;
} else {
// temp.next = new ListNode();
// temp = temp.next ; writing two times in if write it above to create node
temp.val = sum;
carry = 0;
}
temp1 = temp1.next;
}
while (temp2 != null) {
int sum = temp2.val + carry;
// new node creation
temp.next = new ListNode();
temp = temp.next;
if (sum >= 10) {
// temp.next = new ListNode();
// temp = temp.next ; writing two times in if write it above to create node
temp.val = sum % 10;
carry = sum / 10;
} else {
// temp.next = new ListNode();
// temp = temp.next ; writing two times in if write it above to create node
temp.val = sum;
carry = 0;
}
temp2 = temp2.next;
}
if (carry > 0) {
temp.next = new ListNode();
temp = temp.next;
temp.val = carry;
temp.next = null;
}
ListNode reversedummy = reverse(dummy);
return reversedummy;
}
public static ListNode reverse(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prev = null;
ListNode first = head;
ListNode second = head.next;
while (first != null) {
first.next = prev;
prev = first;
first = second;
if (second != null) {
second = second.next;
}
}
return prev;
}
}
// Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}