-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
147 lines (128 loc) · 2.99 KB
/
Copy pathQuestion2.java
File metadata and controls
147 lines (128 loc) · 2.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
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 2
*/
public class Question2
{
public static int intValueOfChar(char ch)
{
return (int)(ch)-48;
}
public static String addString(String s1, String s2)
{
StringBuilder result = new StringBuilder();
int diff = Math.abs(s1.length()-s2.length());
if(s1.length()<s2.length())
for(int i = 0;i<diff;i++)
s1 = '0'+s1;
else
for(int i = 0;i<diff;i++)
s2 = '0'+s2;
//System.out.println(s1);
//System.out.println(s2);
int carry = 0;
int val = 0;
int temp = 0;
for(int i = s1.length()-1;i>=0;i--)
{
val = carry + intValueOfChar(s1.charAt(i)) + intValueOfChar(s2.charAt(i));
temp = val;
val = val % 10;
carry = temp/10;
result.append(val);
}
if(carry!=0)
result.append(carry);
//result.reverse();
return result.toString();
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();
ListNode h1 = l1;
ListNode h2 = l2;
while(l1.next!= null)
{
s1.append(l1.val);
l1 = l1.next;
}
s1.append(l1.val);
s1.reverse();
//System.out.println(s1.toString());
while(l2.next!= null)
{
s2.append(l2.val);
l2 = l2.next;
}
s2.append(l2.val);
s2.reverse();
//System.out.println(s2.toString());
String s3 = addString(s1.toString(),s2.toString());
//System.out.println(s3);
//Converting s3 string to linked list
ListNode head = new ListNode();
ListNode temp = head;
ListNode newNode;
for(int i = 0;i<s3.length();i++)
{
head.val = intValueOfChar(s3.charAt(i));
if(i==s3.length()-1)
break;
newNode = new ListNode();
head.next = newNode;
head = head.next;
}
head.next = null;
//System.out.println(s3);
return temp;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
//System.out.println(addString("99999","9999"));
ListNode a1 = new ListNode();
ListNode a2 = new ListNode();
ListNode a3 = new ListNode();
ListNode a4 = new ListNode();
ListNode a5 = new ListNode();
ListNode a6 = new ListNode();
ListNode a7 = new ListNode();
a1.val = 2;
a1.next = a2;
a2.val = 4;
a2.next = a3;
a3.val = 3;
a3.next = null;
a4.val = 9;
a4.next = a5;
a5.val = 9;
a5.next = a6;
a6.val = 9;
a6.next = a7;
a7.val = 9;
a7.next = null;
ListNode b1 = new ListNode();
ListNode b2 = new ListNode();
ListNode b3 = new ListNode();
ListNode b4 = new ListNode();
b1.val = 5;
b1.next = b2;
b2.val = 6;
b2.next = b3;
b3.val = 4;
b3.next = null;
b4.val = 9;
b4.next = null;
ListNode result = new ListNode();
result = addTwoNumbers(a1,b1);
while(result.next!=null)
{
//System.out.println(result.val);
result = result.next;
}
//System.out.println(result.val);
//System.out.println(result.next);
}
}