Open In App

JavaScript Program to Swap Every Two Adjacent Nodes in a Linked List

Given a linked list, our objective is to swap every two adjacent nodes in JavaScript. This program defines a Node class representing elements in the list and provides both iterative and recursive methods to achieve the swapping functionality.

The iterative approach adjusts pointers to swap adjacent nodes, while the recursive approach manipulates node connections to achieve the same result.

Example:

Input: 
1 -> 2 -> 3 -> 4
Output:
2 -> 1 -> 4 -> 3.

Using Iterative Method

In this approach, Define a Node class for creating linked list nodes and a function to swap every two adjacent nodes. It iterates through the linked list, swapping adjacent nodes while updating pointers accordingly. The original list is displayed, and then the list after swapping adjacent nodes is shown using an iterative approach.

Example: The example below shows how to Swap every two Adjacent Nodes in a Linked List using an Iterative Approach.

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

function swapAdjacentNodes(head) {
    if (!head || !head.next) {
        return head;
    }

    let prev = null;
    let current = head;
    let next = null;
    let newHead = head.next;

    while (current && current.next) {
        next = current.next;

        current.next = next.next;
        next.next = current;

        if (prev) {
            prev.next = next;
        }

        prev = current;
        current = current.next;
    }

    return newHead;
}

const list = new Node(1);
list.next = new Node(2);
list.next.next = new Node(3);
list.next.next.next = new Node(4);

console.log('Original List:');
let current = list;
while (current) {
    console.log(current.data);
    current = current.next;
}

const iterativeSwappedList = 
    swapAdjacentNodes(list);
console.log('\nList after swapping using Iterative Approach:');
current = iterativeSwappedList;
while (current) {
    console.log(current.data);
    current = current.next;
}

Output
Original List:
1
2
3
4

List after swapping using Iterative Approach:
2
1
4
3

Time Complexity: O(n), Where n is the number of nodes in the linked list.

Space Complexity: O(1)

Using Recursive Method

In this approach, Define a Node class for creating linked list nodes and a function to swap every two adjacent nodes recursively. It swaps adjacent nodes by recursively calling the function with the next. next node and adjusting pointers accordingly. The original list is displayed, and then the list after swapping adjacent nodes is shown using a recursive approach.

Example: The example below shows how to Swap every two Adjacent Nodes in a Linked List using a Recursive Approach.

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

function swapAdjacentNodesRecursive(head) {
    if (!head || !head.next) {
        return head;
    }

    let next = head.next;
    head.next = swapAdjacentNodesRecursive(next.next);
    next.next = head;

    return next;
}

// Input
const list = new Node(1);
list.next = new Node(2);
list.next.next = new Node(3);
list.next.next.next = new Node(4);

console.log('Original List:');
let current = list;
while (current) {
    console.log(current.data);
    current = current.next;
}

const recursiveSwappedList = 
    swapAdjacentNodesRecursive(list);

console.log('\nList after swapping using Recursive Approach:');
current = recursiveSwappedList;
while (current) {
    console.log(current.data);
    current = current.next;
}

Output
Original List:
1
2
3
4

List after swapping using Recursive Approach:
2
1
4
3

Time Complexity: O(n), Where n is the number of nodes in the linked list.

Space Complexity: O(n)

Article Tags :