-
Notifications
You must be signed in to change notification settings - Fork 13
/
LL_AddTwoNumRepresentedByLL.java
103 lines (87 loc) · 3.24 KB
/
LL_AddTwoNumRepresentedByLL.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
// https://practice.geeksforgeeks.org/problems/add-two-numbers-represented-by-linked-lists/1
// https://www.geeksforgeeks.org/sum-of-two-linked-lists/
// https://leetcode.com/problems/add-two-numbers-ii/submissions/
// https://leetcode.com/problems/add-two-numbers/ (similar problem)
// https://www.youtube.com/watch?v=HzWKOp0C_aU (a video explanation)
// https://leetcode.com/problems/add-two-numbers-ii/discuss/92623/Easy-O(n)-Java-Solution-using-Stack (Similar Approach)
public class LL_AddTwoNumRepresentedByLL {
private static Node addList(Node head1, Node head2) {
if(head1 == null) return head2;
if(head2 == null) return head1;
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
while (head1 != null) {
stack1.push(head1.data);
head1 = head1.next;
}
while (head2 != null) {
stack2.push(head2.data);
head2 = head2.next;
}
Node dummyHead = new Node(0);
Node currNode = dummyHead;
int sum = 0;
int carry = 0;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if(!stack1.isEmpty()) sum += stack1.pop();
if(!stack2.isEmpty()) sum += stack2.pop();
Node newNode = new Node(sum % 10);
newNode.next = currNode.next;
currNode.next = newNode;
carry = sum / 10;
sum = carry;
}
if(carry != 0) {
Node newNode = new Node(carry);
newNode.next = currNode.next;
currNode.next = newNode;
}
return dummyHead.next;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
int n = Integer.parseInt(br.readLine().trim());
String[] str1 = br.readLine().trim().split("\\s+");
int m = Integer.parseInt(br.readLine().trim());
String[] str2 = br.readLine().trim().split("\\s+");
Node head1 = buildLinkedList(str1);
Node head2 = buildLinkedList(str2);
printLinkedList(head1);
printLinkedList(head2);
Node resultHead = addList(head1, head2);
printLinkedList(resultHead);
}
}
private static void printLinkedList(Node head) {
StringBuilder sb = new StringBuilder();
while(head != null) {
sb.append(head.data).append(" ");
head = head.next;
}
System.out.println(sb);
sb.setLength(0);
}
private static Node buildLinkedList(String[] str) {
Node head = new Node(Integer.parseInt(str[0]));
Node currNode = head;
for (int i = 1; i < str.length; i++) {
currNode.next = new Node(Integer.parseInt(str[i]));
currNode = currNode.next;
}
return head;
}
static class Node {
private final int data;
private Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
}