Given 1->2->3->4,
reverseInPairs(1) ==> 2->1->4->3
List
, two nodes at a time and swap the data between adjacent nodes.
1. Create a pointer for the head
node.
2. While the current node curr
and next
are not null, traverse the List
3. Swap the data between the two nodes.
4. Increment the pointers by two nodes to fetch next pair.
public ListNode reverseInPairs(ListNode head) { }
C
Java
Python