JavaScript Program for Pairwise Swapping Elements of a Given Linked List

In this tutorial, we will learn how to implement a JavaScript program for pairwise swapping elements of a given linked list. This operation swaps adjacent elements in pairs, which can be useful for reorganizing data, rearranging elements, or optimizing algorithms.

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 understand how to implement pairwise swapping in a linked list using JavaScript.

Problem Statement

Given a linked list, the task is to swap elements pairwise. Elements at consecutive positions are swapped with each other. If the number of elements is odd, the last element remains unchanged. The program should return the head of the modified linked list.

Examples

Example 1:

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

Explanation: Elements at positions 0 and 1 (1 and 2) are swapped, then elements at positions 2 and 3 (3 and 4) are swapped. The last element (5) remains unchanged as it has no pair.

Example 2:

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

Explanation: Each pair of adjacent elements is swapped: (10,20), (30,40), and (50,60). The last element (70) remains unchanged.

Algorithm

  1. Create a function pairwiseSwap(head) that takes the head of the linked list as input
  2. Initialize a temporary variable temp to store the current node, set to the head
  3. Loop through the linked list, processing two nodes at a time
  4. For each pair of nodes, swap their values
  5. Move to the next pair of nodes
  6. Continue until the end of the list or no more pairs exist
  7. Return the head of the modified linked list

Implementation

Here's the complete JavaScript implementation with a working example:

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

function pairwiseSwap(head) {
   let temp = head;
   
   // Traverse the list and swap adjacent pairs
   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;
   }
   
   return head;
}

function printList(head, label) {
   console.log(label);
   let temp = head;
   let result = "";
   while (temp !== null) {
      result += temp.value + " -> ";
      temp = temp.next;
   }
   result += "null";
   console.log(result);
}

// Create linked list: 1 -> 2 -> 3 -> 4 -> 5
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);

printList(head, "Original Linked List:");

// Perform pairwise swapping
head = pairwiseSwap(head);

printList(head, "Linked List after Pairwise Swapping:");
Original Linked List:
1 -> 2 -> 3 -> 4 -> 5 -> null
Linked List after Pairwise Swapping:
2 -> 1 -> 4 -> 3 -> 5 -> null

How It Works

The algorithm works by iterating through the linked list and swapping the values of every two adjacent nodes:

  1. Start with the first node (temp = head)
  2. Check if both current node and next node exist
  3. Swap their values using a temporary variable
  4. Move two positions ahead (temp.next.next)
  5. Repeat until no more pairs exist

Time and Space Complexity

  • Time Complexity: O(n), where n is the number of nodes in the linked list
  • Space Complexity: O(1), as we only use a constant amount of extra space

Conclusion

This JavaScript program efficiently swaps adjacent elements in a linked list using an in-place algorithm. The solution maintains O(1) space complexity while processing all pairs in O(n) time, making it an optimal approach for pairwise swapping operations.

Updated on: 2026-03-15T23:19:01+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements