JavaScript Program for Pairwise Swapping Elements of a Given Linked List

In this tutorial, we are going to learn about a JavaScript Program for Pairwise Swapping Elements of a Given Linked List. One common operation on linked lists is swapping adjacent elements in pairs. This operation can be useful in various scenarios, such as reorganizing data, rearranging elements in a specific order, or optimizing certain algorithms. Also, we will focus on solving the problem of pairwise swapping elements in a given linked list using JavaScript. We will provide a step-by-step approach to implementing the algorithm, explaining the logic and code behind it. By the end of this tutorial, you will have a clear understanding of how to implement a JavaScript program to pairwise swap elements in a linked list, along with sample code and explanations for each step.

Let's dive in and explore the solution to this problem in JavaScript!

Problem Statement

Given a linked list, the task is to implement a JavaScript program that swaps elements pairwise. In other words, the elements at consecutive positions in the linked list are to be swapped with each other. If the number of elements in the linked list is odd, then the last element remains unchanged. The program should return the head of the modified linked list.

Sample Examples

Example 1

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


Explanation − In the given linked list, the elements at positions 1 and 2 (1 and 2 are 0-indexed) are swapped, resulting in 2 -> 1 -> 3 -> 4 -> 5. Then, the elements at positions 3 and 4 are swapped, resulting in 2 -> 1 -> 4 -> 3 -> 5.

Example 2

Input: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70
Output: 20 -> 10 -> 40 -> 30 -> 60 -> 50 -> 70


Explanation  In the given linked list, the elements at positions 1 and 2 are swapped, resulting in 20 -> 10 -> 30 -> 40 -> 50 -> 60 -> 70. Then, the elements at positions 3 and 4 are swapped, resulting in 20 -> 10 -> 40 -> 30 -> 50 -> 60 -> 70. Finally, the elements at positions 5 and 6 are swapped, resulting in 20 -> 10 -> 40 -> 30 -> 60 -> 50 -> 70.

Now, let’s understand the algorithm for implementing this problem statement.

Algorithm

• Initialize a temporary variable temp to store the current node, and set it to the head of the linked list.

• Loop through the linked list with a step of 2, i.e., move two nodes at a time.

• For each pair of nodes, swap their values.

• Move to the next pair of nodes.

• Continue this process until the end of the linked list is reached or there are no more pairs to swap.

So, after understanding the algorithm let’s implement this algorithm with the help of an example where we implement this algorithm with the help of JavaScript.

Example: Implementation Using JavaScript

The program above implements pairwise swapping of elements in a given linked list. It uses a Node class to represent the nodes of the linked list, and a pairwiseSwap() function to swap the values of adjacent nodes in pairs. The program first creates a linked list with a given set of elements, displays the original linked list, performs pairwise swapping using the pairwiseSwap() function, and then displays the updated linked list with swapped elements.

Input: Original Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> null

Expected Output: Linked List after Pairwise Swapping: 2 -> 1 -> 4 -> 3 -> 5 -> null

class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
while (temp !== null && temp.next !== null) {
// Swap values of current and next nodes
let tempVal = temp.value;
temp.value = temp.next.value;
temp.next.value = tempVal;
// Move to the next pair of nodes
temp = temp.next.next;
}
}

// Linked list with odd number of elements
let node2 = new Node(2);
let node3 = new Node(3);
let node4 = new Node(4);
let node5 = new Node(5);
node2.next = node3;
node3.next = node4;
node4.next = node5;
while (temp !== null) {
process.stdout.write(temp.value + " -> ");
temp = temp.next;
}
console.log("null");
while (temp !== null) {
process.stdout.write(temp.value + " -> ");
temp = temp.next;
}
console.log("null");


Conclusion

To sum up, the JavaScript program provided in this tutorial demonstrates an efficient solution for the pairwise swapping of elements in a given linked list. The algorithm iterates through the linked list, swapping adjacent elements in pairs, resulting in an updated linked list with swapped elements. This solution can be useful in various scenarios where element swapping is required in linked list operations. By implementing this program, one can easily perform pairwise swapping of elements in a linked list using JavaScript.

Updated on: 02-May-2023

120 Views