24 Swap Nodes in Pairs

Intuition & Algorithm

Question 25 is a more general question of this one.

Suppose there are three pairs $p_0$, $p_1$ and $p_2$. We need to swap nodes in $p_1$, after that, we should reconnect it to it’s adjacent pairs by:

For the first pair, $p_0(1)=null$ so we don’t need the first equation and and for the last one, $p_2(0)=null$;

Code

  1. Recursion
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode swapPairs(ListNode head) {
    return dp(head);
    }

    private ListNode dp(ListNode head){
    if(head == null || head.next == null)
    return head;
    ListNode next = head.next.next;
    head.next.next = head;
    head = head.next;
    head.next.next = dp(next);
    return head;
    }
    }

2.Iteration

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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode gH = head;
ListNode gT = head.next;
head = gT;
while(gH != null && gT != null){
ListNode tmp = gT.next;
gT.next = gH;
if(tmp != null && tmp.next != null){
gH.next = tmp.next;
gH = tmp; gT = tmp.next;
}
else{
gH.next = tmp;
gH = tmp; gT = null;
}
}
return head;
}
}

Complexity

Time Complexity: $O(n)$ becasue we iterate over the entire list

Space Complexity: For the recursion solution, $O(\lceil n/2 \rceil)$ space is necessary to store all the pairs. We cost constant space $O(1)$ for the iteration method.