-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumTwinSumOfLinkedList.java
More file actions
215 lines (165 loc) · 5.05 KB
/
MaximumTwinSumOfLinkedList.java
File metadata and controls
215 lines (165 loc) · 5.05 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package Algorithms.LinkedListAlgos;
import java.util.ArrayList;
import java.util.List;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 23 April 2025
*
* 0 1 2 3 4 5 6 7 null
*
* (n-1-i)th is the twin of ith node
* if n=8
* if i=0, twin=(8-1-0)=7 -- i0 & i7
* if i=1, twin=(8-1-1)=6 -- i1 & i6
* if i=2, twin=(8-1-2)=5 -- i2 & i5
* if i=3, twin=(8-1-3)=4 -- i3 & i4
*
*
*/
public class MaximumTwinSumOfLinkedList {
static class ListNode{int val; ListNode next; ListNode(int x) { val = x; } ListNode(int x, ListNode next) { val = x; this.next = next; }}
public static void main(String[] args) {
ListNode head = new ListNode(5, new ListNode(4, new ListNode(2, new ListNode(1))));
System.out.println( "pairSum(head) => " + pairSum(head)); // Output: 6 (5 + 1)
}
public int pairSumUsingLst(ListNode head) {
if(head.next.next==null) return head.val + head.next.val;
ListNode s = head, f = head;
List<Integer> lst = new ArrayList<>();
while(f!=null) {
lst.add(s.val);
s=s.next;
f=f.next.next;
}
int max = 0;
for(int i=lst.size()-1; s!=null; i--, s=s.next) {
// System.out.printf("lst num: %s, s.val: %s \n", lst.get(i), s.val);
max=Math.max(max, (lst.get(i)+s.val));
}
return max;
}
public int pairSumMyApproach(ListNode head) {
if(head.next.next==null) return head.val + head.next.val;
ListNode s = head, f = head;
while(f.next.next!=null) {
s=s.next;
f=f.next.next;
}
ListNode temp = s.next;
s.next = null; // break connection
s=temp;
// reverse head
ListNode prev = null, curr = head;
while(curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
// head is now reversed
int max = 0;
while(s!=null) {
max = Math.max(max, head.val+s.val);
s=s.next;
head=head.next;
}
return max;
}
public static int pairSum(ListNode head) {
ListNode prev = null;
ListNode s = head;
ListNode f = head;
// combination of slow, fast pointer and reverse linked list
while (f != null && f.next != null) {
f = f.next.next;
ListNode next = s.next;
s.next = prev;
prev = s;
s = next;
}
int max = 0;
// now prev is in reverse order
while (s != null) {
max = Math.max(max, s.val + prev.val);
s = s.next;
prev = prev.next;
}
return max;
}
public static int pairSum2(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
ListNode SecondHalf = reverse(slow);
int maxSum=0;
ListNode first = head,second = SecondHalf;
while(second!=null){
int twinSum = first.val + second.val;
maxSum = Math.max(twinSum,maxSum);
first =first.next;
second = second.next;
}
return maxSum;
}
private static ListNode reverse(ListNode head){
ListNode prev=null;
ListNode curr = head;
while(curr!=null){
ListNode next = curr.next;
curr.next=prev;
prev = curr;
curr = next;
}
return prev;
}
public static int pairSum3(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null;
while (slow != null) {
ListNode next = slow.next;
slow.next = prev;
prev = slow;
slow = next;
}
int maxSum = 0;
while (prev != null) {
maxSum = Math.max(maxSum, head.val + prev.val);
head = head.next;
prev = prev.next;
}
return maxSum;
}
public int pairSum4(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null;
ListNode current = slow;
while (current != null) {
ListNode nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
ListNode firstHalf = head;
ListNode secondHalf = prev;
int maxTwinSum = 0;
while (secondHalf != null) {
maxTwinSum = Math.max(maxTwinSum, firstHalf.val + secondHalf.val);
firstHalf = firstHalf.next;
secondHalf = secondHalf.next;
}
return maxTwinSum;
}
}