classSolution{publicintpairSum(ListNode head){ListNode slow = head;ListNode fast = head;int res =Integer.MIN_VALUE;// Find the middle positionwhile(fast !=null&& fast.next !=null){
slow = slow.next;
fast = fast.next.next;}ListNode nextNode, prev =null;// Reverse the second half listwhile(slow !=null){
nextNode = slow.next;// Reverse the arrow, point to the previous node
slow.next = prev;// Move 1 step to the right
prev = slow;
slow = nextNode;}// Calculate all pairs sumwhile(prev !=null){
res =Math.max(res, head.val + prev.val);
prev = prev.next;
head = head.next;}return res;}}