-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumLL.java
More file actions
135 lines (89 loc) · 2.83 KB
/
Copy pathSumLL.java
File metadata and controls
135 lines (89 loc) · 2.83 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
package linkedlist;
class Some{
LinkedListNode<Integer> head;
int carry;
Some(LinkedListNode<Integer> head,int carry){
this.head=head;
this.carry=carry;
}
}
public class SumLL {
public static int length(LinkedListNode<Integer> head) {
LinkedListNode<Integer> temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
public static Some helper2(LinkedListNode<Integer> head1, LinkedListNode<Integer> temp,int carry,LinkedListNode<Integer> head){
int sum=0;
if (head != temp) {
Some smallans = helper2(head1.next,temp,carry,head);
sum = smallans.carry + head1.data;
carry = sum / 10;
sum %= 10;
LinkedListNode<Integer> newnode = new LinkedListNode<Integer>(sum);
newnode.next = smallans.head;
return new Some(newnode,carry);
}
return new Some(head,carry);
}
public static Some helper(LinkedListNode<Integer> h1, LinkedListNode<Integer> h2, int carry) {
if (h1 == null) {
return new Some(null,0);
}
Some smallans = helper(h1.next, h2.next, carry);
int sum = (h1.data + h2.data + smallans.carry);
carry = sum / 10;
sum %= 10;
LinkedListNode<Integer> currhead = new LinkedListNode(sum);
currhead.next = smallans.head;
return new Some(currhead,carry);
}
public static LinkedListNode<Integer> addNumbers(LinkedListNode<Integer> head1, LinkedListNode<Integer> head2) {
LinkedListNode<Integer> head = null;
LinkedListNode<Integer> temp = null;
if (head1 == null) {
head = head2;
return head;
} else if (head2 == null) {
head = head1;
return head;
}
int len1 = length(head1);
int len2 = length(head2);
int carry = 0;
Some p = null;
if (len1 == len2) {
p = helper(head1, head2, 0);
carry = p.carry;
head = p.head;
} else {
int diff = Math.abs(len1 - len2);
if (len1 < len2) {
LinkedListNode<Integer> t = head1;
head1 = head2;
head2 = t;
}
temp = head1;
while (diff > 0) {
temp = temp.next;
diff -= 1;
}
p = helper(temp, head2, 0);
carry = p.carry;
head = p.head;
p=helper2(head1,temp,carry,head);
head=p.head;
carry=p.carry;
}
if (carry > 0) {
LinkedListNode<Integer> tempNode = new LinkedListNode(carry);
tempNode.next = head;
head = tempNode;
}
return head;
}
}