Open In App

Javascript Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links

Last Updated : 05 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5

This problem has been discussed here. The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. So changing links is a better idea in general. Following is the implementation that changes links instead of swapping data. 

JavaScript
// Javascript program to swap elements 
// of linked list by changing links
let head;

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

/* Function to pairwise swap elements 
   of a linked list */
function pairWiseSwap(node) {
    // If linked list is empty or there 
    // is only one node in list
    if (node == null ||
        node.next == null) {
        return node;
    }

    // Initialize previous and current 
    // pointers
    let prev = node;
    let curr = node.next;

    // Change head before proceeding
    node = curr;

    // Traverse the list
    while (true) {
        let next = curr.next;

        // Change next of current as 
        // previous node
        curr.next = prev;

        // If next NULL or next is the 
        // last node
        if (next == null ||
            next.next == null) {
            prev.next = next;
            break;
        }

        // Change next of previous to next 
        // next
        prev.next = next.next;

        // Update previous and curr
        prev = next;
        curr = prev.next;
    }
    return node;
}

/* Function to print nodes in a 
   given linked list */
function printList(node) {
    while (node != null) {
        console.log(node.data + " ");
        node = node.next;
    }
}

// Driver code
/* The constructed linked list is: 
   1->2->3->4->5->6->7 */
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.next.next.next.next.next =
    new Node(6);
head.next.next.next.next.next.next =
    new Node(7);

console.log("Linked list before calling pairwiseSwap() ");
printList(head);
let st = pairWiseSwap(head);
console.log("Linked list after calling pairwiseSwap() ");
printList(st);
console.log("");
// This code is contributed by aashish1995 

Output
Linked list before calling pairwiseSwap() 
1 
2 
3 
4 
5 
6 
7 
Linked list after calling pairwiseSwap() 
2 
1 
4 
3 
6 
5 
7 

Complexity Analysis:

  • Time Complexity: The time complexity of the above program is O(n) where n is the number of nodes in a given linked list. The while loop does a traversal of the given linked list.
  • Auxiliary Space: O(1)

Following is the recursive implementation of the same approach. We change the first two nodes and recur for the remaining list. Thanks to geek and omer salem for suggesting this method. 

JavaScript
// Javascript program to swap elements of
// linked list by changing links
let head;
class Node 
{
    constructor(val) 
    {
        this.data = val;
        this.next = null;
    }
}

/* Function to pairwise swap elements of 
   a linked  It returns head of the modified 
   list, so return value of this node must be 
   assigned */
function pairWiseSwap(node) 
{
    // Base Case: The list is empty or has 
    // only one node
    if (node == null || 
        node.next == null) 
    {
        return node;
    }

// Store head of list after 
// two nodes
let remaining = node.next.next;

// Change head
let newhead = node.next;

// Change next of second node
node.next.next = node;

// Recur for remaining list and 
// change next of head
node.next = pairWiseSwap(remaining);

// Return new head of modified list
return newhead;
}

/* Function to print nodes in a 
   given linked list */
function printList(node) 
{
    while (node != null) 
    {
        console.log(node.data);
        node = node.next;
    }
}

// Driver code
/* The constructed linked list is: 
   1->2->3->4->5->6->7 */
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.next.next.next.next.next = 
new Node(6);
head.next.next.next.next.next.next = 
new Node(7);

console.log("Linked list before calling pairwiseSwap() ");
printList(head);
head = pairWiseSwap(head);
console.log("Linked list after calling pairwiseSwap() ");
printList(head);
// This code is contributed by umadevi9616

Output
Linked list before calling pairwiseSwap() 
1
2
3
4
5
6
7
Linked list after calling pairwiseSwap() 
2
1
4
3
6
5
7

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(n)

Please refer complete article on Pairwise swap elements of a given linked list by changing links for more details!



Next Article
Practice Tags :

Similar Reads