-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
187 lines (146 loc) · 4.12 KB
/
AddTwoNumbers.java
File metadata and controls
187 lines (146 loc) · 4.12 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
179
180
181
182
183
184
185
186
187
import java.util.Scanner;
public class AddTwoNumbers {
static int l1=0,l2=0;
static ListNode rootSum = null;
static ListNode root1 =null;
static ListNode root2 =null;
private static class ListNode{
private int data;
private ListNode next;
ListNode(int d)
{
this.data = d;
this.next = null;
}
static void add(int d,int i){
ListNode newNode = new ListNode(d);
if(i==1){
l1++;
ListNode temp = root1;
if(root1==null){
root1 = newNode;
}
else
{
while(temp.next!=null)
{
temp=temp.next;
}
temp.next= newNode;
}
}
else{
l2++;
ListNode temp = root2;
if(root2==null){
root2 = newNode;
}
else
{
while(temp.next!=null)
{
temp=temp.next;
}
temp.next= newNode;
}
}
}
static void addStart(int d,int i)
{
if(i==1){
ListNode newNode = new ListNode(d);
newNode.next = root1;
root1 = newNode;
l1++;
}
else{
ListNode newNode = new ListNode(d);
newNode.next = root2;
root2 = newNode;
l2++;
}
}
static void display(int i){
if(i==1){
ListNode temp=root1;
while(temp!=null){
System.out.print(temp.data +" ");
temp = temp.next;
}
}
else{
ListNode temp=root2;
while(temp!=null){
System.out.print(temp.data +" ");
temp = temp.next;
}
}
}
// static sumLinked(){}
static void sum()
{
int b[] = new int[l2+2];
int sum=0,t=0;
System.out.print("\n[");
while(root1!=null){
if(t==0) {
sum = sum + root1.data + root2.data;
}
else
sum = sum + root1.data + root2.data + b[t-1];
if(sum >= 10){
if(root1.next!=null){
sum = sum%10;
b[t]=1;
t++;
}
else if(root1.next == null && sum >=10){
System.out.print((sum%10)+","+1);
break;
}
}
else{
b[t]=0;
t++;
}
root2=root2.next;
root1 = root1.next;
System.out.print(sum);
if(root1!=null)
System.out.print(",");
sum=0;
}
System.out.print("]");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a,i;
for(i=1;i<=2;i++){
a=sc.nextInt();
while(a!=0)
{
add(a%10,i);
a=a/10;
}
display(i);
}
int len1=l1,len2=l2;
if(len1>len2){
for(int x = 1;x <= (len1-len2) ; x++){
add(0,2);
}
// System.out.println();
// display(2);
}
else if(len2>len1){
for(int x = 1;x <= (len2-len1) ; x++){
add(0,1);
}
}
// ListNode temp = (l1>l2) ? root1:root2;
// System.out.println("\nlen "+l1+" "+l2);
// System.out.println("diff "+(l1-l2));
sum();
}
}
}