-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo Linked List Sum.java
More file actions
65 lines (56 loc) · 1.98 KB
/
Two Linked List Sum.java
File metadata and controls
65 lines (56 loc) · 1.98 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
public class Test{
public static void main(String[] args) throws IOException {
LinkedList<Integer> linkedList1 = new LinkedList<Integer>();
LinkedList<Integer> linkedList2 = new LinkedList<Integer>();
LinkedList<Integer> linkedList3 = new LinkedList<Integer>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String lines = br.readLine();
String[] strs = lines.trim().split("\\s+");
for (int i = 0; i< strs.length; i++){
linkedList1.add(Integer.parseInt(strs[i]));
}
lines = br.readLine();
strs = lines.trim().split("\\s+");
for (int i = 0; i< strs.length; i++){
linkedList2.add(Integer.parseInt(strs[i]));
}
int carry = 0;
if(linkedList1.size()>=linkedList2.size()){
Iterator<Integer> iter = linkedList2.iterator();
for(int i:linkedList1){
if(iter.hasNext()){
int value = iter.next();
linkedList3.add((carry+i+value)%10);
carry = (i+value)/10;
}
else {
linkedList3.add((parity+i)%10);
carry = (i+carry)/10;
}
}
}
else {
Iterator<Integer> iter = linkedList1.iterator();
for(int i:linkedList2){
if(iter.hasNext()){
int value = iter.next();
linkedList3.add((carry+i+value)%10);
carry = (i+value)/10;
}
else {
linkedList3.add((carry+i)%10);
carry = (i+parity)/10;
}
}
}
if(carry>0){
linkedList3.add(carry);
}
System.out.println(linkedList3);
}
}