Posts Swap Nodes in Pairs
Post
Cancel

Swap Nodes in Pairs

PROBLEM DESCRIPTION

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)

leetcode

SOLUTION

APPROACH 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {

    public ListNode swapPairs(ListNode head) {

        if(head == null || head.next == null) return head;

        ListNode h1 = head;
        ListNode h2 = head.next;
        ListNode h3 = head.next.next;

        //Next node points to first
        h2.next = h1;

        //First Node points to swapPairs(Third Node)
        h1.next = swapPairs(h3);

        //Head will be second node
        return h2;
        
    }
    
}

APPROACH 2

Using the algorithm to reverse nodes in groups, we can pass the size of group as 2.

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
class Solution {
    public ListNode swapPairs(ListNode head) {
        return reverseKGroups(head, 2);
    }

    public ListNode reverseKGroups(ListNode head, int k){

        if(head == null || k == 0) return head;

        ListNode h1 = head;
        ListNode h2 = null;
        ListNode t = head;
        int times = k;

        ListNode h3 = head;

        while(k>0 && h1 != null){
            k--;
            h1 = h1.next;
            t.next = h2;
            h2 = t;
            t = h1;
        }

        h3.next = reverseKGroups(h1, times);

        return h2;

    }
}
This post is licensed under CC BY 4.0 by the author.