-
Notifications
You must be signed in to change notification settings - Fork 7
/
MaxTwinSumCalculator.java
67 lines (53 loc) · 1.76 KB
/
MaxTwinSumCalculator.java
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
package linked_list;
public class MaxTwinSumCalculator {
static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
public int pairSum(ListNode head) {
ListNode slow = head, fast = head;
int maxSum = 0;
// Finding the middle of the list
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// Reversing the second half of the list
ListNode nextNode, prev = null;
while (slow != null) {
nextNode = slow.next;
slow.next = prev;
prev = slow;
slow = nextNode;
}
// Calculating max twin sum
while (prev != null) {
maxSum = Math.max(maxSum, head.val + prev.val);
head = head.next;
prev = prev.next;
}
return maxSum;
}
private static ListNode arrayToList(int[] arr) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int num : arr) {
tail.next = new ListNode(num);
tail = tail.next;
}
return dummy.next;
}
public static void main(String[] args) {
MaxTwinSumCalculator calculator = new MaxTwinSumCalculator();
ListNode head1 = arrayToList(new int[]{5,4,2,1});
assert calculator.pairSum(head1) == 6 : "Test case 1 failed";
ListNode head2 = arrayToList(new int[]{4,2,2,3});
assert calculator.pairSum(head2) == 7 : "Test case 2 failed";
ListNode head3 = arrayToList(new int[]{1,100000});
assert calculator.pairSum(head3) == 100001 : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}