-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2p5.java
More file actions
58 lines (53 loc) · 1.5 KB
/
Copy pathq2p5.java
File metadata and controls
58 lines (53 loc) · 1.5 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
import java.util.*;
import java.lang.*;
public class q2p5{
public static void main(String[] args){
Node head1 = new Node(7);
head1.append(1);
head1.append(6);
Node head2 = new Node(5);
head2.append(9);
head2.append(2);
Node buf1 = head1;
Node buf2 = head2;
StringBuilder sb = new StringBuilder();
while(buf1 != null){
sb.append(buf1.value);
buf1 = buf1.next;
}
String str1 = sb.reverse().toString();
sb = new StringBuilder();
while(buf2 != null){
sb.append(buf2.value);
buf2 = buf2.next;
}
String str2 = sb.reverse().toString();
int sum = Integer.valueOf(str1) + Integer.valueOf(str2);
String ans = ((Integer)sum).toString();
char[] arrChar = ans.toCharArray();
Node result = new Node(Integer.valueOf(arrChar[arrChar.length-1])-48);
for(int i = arrChar.length-2; i>=0; i--){
result.append(Integer.valueOf(arrChar[i])-48);
}
buf1 = result;
while(buf1 != null){
System.out.println(buf1.value);
buf1 = buf1.next;
}
}
}
class Node{
public Node next = null;
public int value;
public Node(int value){
this.value = value;
}
public void append(int value){
Node end = new Node(value);
Node buf = this;
while(buf.next != null){
buf = buf.next;
}
buf.next = end;
}
}