Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
How can "reversing" a part of the linked list help find the answer?
We know that the nodes of the first half are twins of nodes in the second half, so try dividing the linked list in half and reverse the second half.
How can two pointers be used to find every twin sum optimally?
Use two different pointers pointing to the first nodes of the two halves of the linked list. The second pointer will point to the first node of the reversed half, which is the (n-1-i)th node in the original linked list. By moving both pointers forward at the same time, we find all twin sums.