Linked List February 22 ,2026

Swap Nodes in Pairs

Problem

Given the head of a Singly Linked List, swap every two adjacent nodes and return the modified head.

You must swap nodes, not just their data.

If the number of nodes is odd, the last node remains unchanged.

Example

Input

1 → 2 → 3 → 4 → 5

Output

2 → 1 → 4 → 3 → 5

Approach 1 — Iterative Pointer Manipulation

Idea

Swap nodes in pairs by adjusting links while traversing the list.

Use a dummy node to simplify handling of the head.

Steps

  1. Create a dummy node pointing to head
  2. Use a pointer prev initially at dummy
  3. While two nodes exist:
    • Let first = prev.next
    • Let second = first.next
  4. Perform swapping:
    • first.next = second.next
    • second.next = first
    • prev.next = second
  5. Move prev = first
  6. Continue until pairs end
  7. Return dummy.next

Time Complexity

O(N)

Space Complexity

O(1)

Input

1 → 2 → 3 → 4 → 5

Output

2 → 1 → 4 → 3 → 5

C Implementation 

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

struct Node* swapPairs(struct Node* head) {
    struct Node dummy;
    dummy.next = head;

    struct Node* prev = &dummy;

    while (prev->next && prev->next->next) {
        struct Node* first = prev->next;
        struct Node* second = first->next;

        // Swapping
        first->next = second->next;
        second->next = first;
        prev->next = second;

        prev = first;
    }

    return dummy.next;
}

void printList(struct Node* head) {
    while (head) {
        printf("%d ", head->data);
        head = head->next;
    }
}

int main() {
    // 1 → 2 → 3 → 4 → 5
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 1;
    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 2;
    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 3;
    head->next->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->next->data = 4;
    head->next->next->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->next->next->data = 5;
    head->next->next->next->next->next = NULL;

    head = swapPairs(head);
    printList(head);

    return 0;
}

Output

2 1 4 3 5

C++ Implementation

// While running this code, replace all #include "..." with #include <...>
#include "iostream"
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = NULL;
    }
};

Node* swapPairs(Node* head) {
    Node dummy(0);
    dummy.next = head;

    Node* prev = &dummy;

    while (prev->next && prev->next->next) {
        Node* first = prev->next;
        Node* second = first->next;

        // Swapping
        first->next = second->next;
        second->next = first;
        prev->next = second;

        prev = first;
    }

    return dummy.next;
}

void printList(Node* head) {
    while (head) {
        cout << head->data << " ";
        head = head->next;
    }
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    head = swapPairs(head);
    printList(head);

    return 0;
}

Output

2 1 4 3 5

Java Implementation

class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

public class Main {

    static Node swapPairs(Node head) {
        Node dummy = new Node(0);
        dummy.next = head;
        Node prev = dummy;

        while (prev.next != null && prev.next.next != null) {
            Node first = prev.next;
            Node second = first.next;

            first.next = second.next;
            second.next = first;
            prev.next = second;

            prev = first;
        }

        return dummy.next;
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);

        head = swapPairs(head);
        printList(head);
    }
}

Output

2 1 4 3 5

Python Implementation 

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def swap_pairs(head):
    dummy = Node(0)
    dummy.next = head
    prev = dummy

    while prev.next and prev.next.next:
        first = prev.next
        second = first.next

        first.next = second.next
        second.next = first
        prev.next = second

        prev = first

    return dummy.next

def print_list(head):
    while head:
        print(head.data, end=" ")
        head = head.next

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

head = swap_pairs(head)
print_list(head)

Output

2 1 4 3 5

JavaScript Implementation 

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function swapPairs(head) {
    let dummy = new Node(0);
    dummy.next = head;
    let prev = dummy;

    while (prev.next && prev.next.next) {
        let first = prev.next;
        let second = first.next;

        first.next = second.next;
        second.next = first;
        prev.next = second;

        prev = first;
    }

    return dummy.next;
}

function printList(head) {
    while (head !== null) {
        process.stdout.write(head.data + " ");
        head = head.next;
    }
}

let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);

head = swapPairs(head);
printList(head);

Output

2 1 4 3 5

Approach 2 — Recursive Swapping

Idea

In this approach, we swap nodes in pairs using recursion.

At each recursive step:

  • Swap the first two nodes
  • Recursively swap the remaining list
  • Connect the swapped pair with the result of recursion

Steps

  1. Base Case
    • If head is NULL OR head.next is NULL
      → return head (no pair to swap)
  2. Identify nodes
    • first = head
    • second = head.next
  3. Recursively swap remaining list starting from second.next
  4. Adjust links
    • first.next = recursiveResult
    • second.next = first
  5. Return second as new head of this pair

Time Complexity

O(N)

Space Complexity

O(N)
(Recursion stack)

Input

1 → 2 → 3 → 4 → 5

Output

2 → 1 → 4 → 3 → 5

C Implementation 

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h" // Corrected

struct Node {
    int data;
    struct Node* next;
};

struct Node* swapPairs(struct Node* head) {
    if (head == NULL || head->next == NULL)
        return head;

    struct Node* first = head;
    struct Node* second = head->next;

    // Recursive call
    first->next = swapPairs(second->next);
    second->next = first;

    return second;
}

void printList(struct Node* head) {
    while (head) {
        printf("%d ", head->data);
        head = head->next;
    }
}

int main() {
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 1;

    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 2;

    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 3;

    head->next->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->next->data = 4;

    head->next->next->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->next->next->data = 5;
    head->next->next->next->next->next = NULL;

    head = swapPairs(head);
    printList(head);

    return 0;
}

Output

2 1 4 3 5

C++ Implementation

// While running this code, replace all #include "..." with #include <...>

#include "iostream"
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = NULL;
    }
};

Node* swapPairs(Node* head) {
    if (head == NULL || head->next == NULL)
        return head;

    Node* first = head;
    Node* second = head->next;

    first->next = swapPairs(second->next);
    second->next = first;

    return second;
}

void printList(Node* head) {
    while (head) {
        cout << head->data << " ";
        head = head->next;
    }
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    head = swapPairs(head);
    printList(head);

    return 0;
}

Output

2 1 4 3 5

Java Implementation

class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

public class Main {

    static Node swapPairs(Node head) {
        if (head == null || head.next == null)
            return head;

        Node first = head;
        Node second = head.next;

        first.next = swapPairs(second.next);
        second.next = first;

        return second;
    }

    static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);

        head = swapPairs(head);
        printList(head);
    }
}

Output

2 1 4 3 5

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def swap_pairs(head):
    if not head or not head.next:
        return head

    first = head
    second = head.next

    first.next = swap_pairs(second.next)
    second.next = first

    return second

def print_list(head):
    while head:
        print(head.data, end=" ")
        head = head.next

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

head = swap_pairs(head)
print_list(head)

Output

2 1 4 3 5

JavaScript Implementation 

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function swapPairs(head) {
    if (head === null || head.next === null)
        return head;

    let first = head;
    let second = head.next;

    first.next = swapPairs(second.next);
    second.next = first;

    return second;
}

function printList(head) {
    while (head !== null) {
        process.stdout.write(head.data + " ");
        head = head.next;
    }
}

let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);

head = swapPairs(head);
printList(head);

Output

2 1 4 3 5

 

Next Problem in the series-

Detect Loop in a Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com