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
| class Solution { public int pairSum(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } slow = reverse(slow); fast = head; int ans = Integer.MIN_VALUE; while (slow != null) { int currSum = slow.val + fast.val; ans = Math.max(ans, currSum); slow = slow.next; fast = fast.next; } return ans; }
private ListNode reverse(ListNode node) { ListNode prev = null, curr = node; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } }
|