/**
* Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int pairSum(ListNode head) {
// Find middle
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse second half
ListNode prev = null;
while (slow != null) {
ListNode next = slow.next;
slow.next = prev;
prev = slow;
slow = next;
}
// Calculate maximum twin sum
int ans = 0;
ListNode first = head;
ListNode second = prev;
while (second != null) {
ans = Math.max(ans, first.val + second.val);
first = first.next;
second = second.next;
}
return ans;
}
}
"""
Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def pairSum(self, head: Optional[ListNode]) -> int:
# Find middle using slow and fast pointers
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Reverse second half of the linked list
prev = None
curr = slow
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
# Calculate maximum twin sum
ans = 0
first = head
second = prev
while second:
ans = max(ans, first.val + second.val)
first = first.next
second = second.next
return ans
/**
* Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <algorithm>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
/**
* Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int pairSum(ListNode* head) {
// Find middle
ListNode* slow = head;
ListNode* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
// Reverse second half
ListNode* prev = nullptr;
ListNode* curr = slow;
while (curr != nullptr) {
ListNode* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
// Calculate maximum twin sum
int ans = 0;
ListNode* first = head;
ListNode* second = prev;
while (second != nullptr) {
ans = max(ans, first->val + second->val);
first = first->next;
second = second->next;
}
return ans;
}
};
/**
* Problem: LeetCode 2130 - Maximum Twin Sum of a Linked List
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {ListNode} head
* @return {number}
*/
var pairSum = function(head) {
// Find middle
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse second half
let prev = null;
let curr = slow;
while (curr !== null) {
let nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
// Calculate maximum twin sum
let ans = 0;
let first = head;
let second = prev;
while (second !== null) {
ans = Math.max(ans, first.val + second.val);
first = first.next;
second = second.next;
}
return ans;
};