-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwinSum.java
More file actions
34 lines (26 loc) · 749 Bytes
/
TwinSum.java
File metadata and controls
34 lines (26 loc) · 749 Bytes
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
package LinkedLists;
import static LinkedLists.ReverseList.reverseList;
public class TwinSum {
public static int pairSum(Node head) {
Node fast = head;
Node slow = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node half = reverseList(slow.next);
Node temp1 = head;
Node temp2 = half;
int ans = 0;
while (temp2 != null) {
if (temp1.data + temp2.data > ans) {
ans = temp1.data + temp2.data;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
return ans;
}
public static void main(String[] args) {
}
}