Home > Web Front-end > JS Tutorial > body text

JavaScript program for pairwise swapping of elements of a given linked list

王林
Release: 2023-09-06 20:45:08
forward
915 people have browsed it

用于成对交换给定链表元素的 JavaScript 程序

In this tutorial, we will learn a JavaScript program for pairwise swapping of elements of a given linked list. A common operation on linked lists is to swap adjacent elements in pairs. This operation is useful in various scenarios, such as reorganizing data, rearranging elements in a specific order, or optimizing certain algorithms. Additionally, we will focus on solving the problem of pairwise swapping of 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 swap elements in a linked list in pairs, along with sample code and instructions for each step.

Let’s dive into the solution to this problem in JavaScript!

Problem Statement

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

Example

Example 1 -

Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 2 -> 1 -> 4 -> 3 -> 5
Copy after login

Explanation- In the given linked list, the elements at position 1 and 2 (1 and 2 are 0 index) are exchanged and the result is 2 -> 1 -> 3 -> 4 -> 5. Then, swap the elements at positions 3 and 4, and the result is 2 -> 1 -> 4 -> 3 -> 5.

Example 2 -

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

Explanation In the given linked list, the elements at positions 1 and 2 are exchanged, and the result is 20 -> 10 -> 30 -> 40 -> 50 -> 60 -> 70. Then, the elements at positions 3 and 4 are swapped, and the result is 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 us understand the algorithm that implements this problem statement.

algorithm

  • Create a function named pairwiseSwap(head), which takes the head of the linked list as input.

  • 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 size of 2, that is, move two nodes at a time.

  • For each pair of nodes, exchange 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.

  • Return the modified linked list header.

So, after understanding the algorithm, let us implement it with the help of an example, in which we implement it with the help of JavaScript.

Example: Using JavaScript

The above program implements pairwise exchange of elements in a given linked list. It uses the Node class to represent the nodes of a linked list and uses the 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 a pairwise swap using the pairwiseSwap() function, and then displays the updated linked list containing the swapped elements.

Input: Original linked list: 1 -> 2 -> 3 -> 4 -> 5 -> null

Expected output: Linked list after pairwise exchange: 2 -> 1 -> 4 -> 3 -> 5 -> null

class Node {
   constructor(value) {
      this.value = value;
      this.next = null;
   }
}
function pairwiseSwap(head) {
   let temp = head;
   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;
}

// Linked list with odd number of elements
let head = new Node(1);
let node2 = new Node(2);
let node3 = new Node(3);
let node4 = new Node(4);
let node5 = new Node(5);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
console.log("Original Linked List:");
let temp = head;
while (temp !== null) {
   process.stdout.write(temp.value + " -> ");
   temp = temp.next;
}
console.log("null");
head = pairwiseSwap(head);
console.log("Linked List after Pairwise Swapping:");
temp = head;
while (temp !== null) {
   process.stdout.write(temp.value + " -> ");
   temp = temp.next;
}
console.log("null");
Copy after login

in conclusion

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

The above is the detailed content of JavaScript program for pairwise swapping of elements of a given linked list. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!