From 7c810fd3b1b23178baf96321326ebcb363146374 Mon Sep 17 00:00:00 2001 From: Pranav Ojha Date: Thu, 12 Oct 2023 11:46:37 +0530 Subject: [PATCH 1/2] pair wise swap --- Data-Structures/Linked-List/pairWiseSwap.js | 105 ++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Data-Structures/Linked-List/pairWiseSwap.js diff --git a/Data-Structures/Linked-List/pairWiseSwap.js b/Data-Structures/Linked-List/pairWiseSwap.js new file mode 100644 index 0000000000..a83f2c95d5 --- /dev/null +++ b/Data-Structures/Linked-List/pairWiseSwap.js @@ -0,0 +1,105 @@ +//problem link leetcode:https://leetcode.com/problems/swap-nodes-in-pairs/ + +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +class LinkedList { + constructor(listOfValues) { + this.headNode = null; + this.tailNode = null; + this.length = 0; + + if (listOfValues instanceof Array) { + for (const value of listOfValues) { + this.addLast(value); + } + } + } + + size() { + return this.length; + } + + head() { + return this.headNode?.data ?? null; + } + + tail() { + return this.tailNode?.data ?? null; + } + + isEmpty() { + return this.length === 0; + } + + addLast(element) { + if (this.headNode === null) { + return this.addFirst(element); + } + const node = new Node(element); + this.tailNode.next = node; + this.tailNode = node; + this.length++; + return this.size(); + } + + addFirst(element) { + const node = new Node(element); + if (this.headNode === null) { + this.tailNode = node; + } + node.next = this.headNode; + this.headNode = node; + this.length++; + return this.size(); + } + + // ... (other methods from the original code) + + pairwiseSwap() { + let current = this.headNode; + let prev = null; + + while (current && current.next) { + const nextNode = current.next; + const nextNextNode = nextNode.next; + + nextNode.next = current; + current.next = nextNextNode; + + if (prev) { + prev.next = nextNode; + } else { + this.headNode = nextNode; + } + + prev = current; + current = nextNextNode; + } + + if (prev) { + this.tailNode = prev; + } + } + + get() { + const list = []; + let currentNode = this.headNode; + while (currentNode) { + list.push(currentNode.data); + currentNode = currentNode.next; + } + return list; + } +} + +// Example usage: +const linkedList = new LinkedList([1, 2, 3, 4, 5]); +console.log("Linked List before pairwise swapping:", linkedList.get()); + +linkedList.pairwiseSwap(); +console.log("Linked List after pairwise swapping:", linkedList.get()); From 4882828ab12f0e2820db222e5d2bc2f8624889bc Mon Sep 17 00:00:00 2001 From: Pranav Ojha Date: Thu, 12 Oct 2023 12:02:17 +0530 Subject: [PATCH 2/2] updated pairwiseswap --- Data-Structures/Linked-List/pairWiseSwap.js | 59 +-------------------- 1 file changed, 2 insertions(+), 57 deletions(-) diff --git a/Data-Structures/Linked-List/pairWiseSwap.js b/Data-Structures/Linked-List/pairWiseSwap.js index a83f2c95d5..dc8d12bb19 100644 --- a/Data-Structures/Linked-List/pairWiseSwap.js +++ b/Data-Structures/Linked-List/pairWiseSwap.js @@ -1,5 +1,5 @@ -//problem link leetcode:https://leetcode.com/problems/swap-nodes-in-pairs/ +//problem link leetcode:https://leetcode.com/problems/swap-nodes-in-pairs/ class Node { constructor(data) { this.data = data; @@ -20,46 +20,6 @@ class LinkedList { } } - size() { - return this.length; - } - - head() { - return this.headNode?.data ?? null; - } - - tail() { - return this.tailNode?.data ?? null; - } - - isEmpty() { - return this.length === 0; - } - - addLast(element) { - if (this.headNode === null) { - return this.addFirst(element); - } - const node = new Node(element); - this.tailNode.next = node; - this.tailNode = node; - this.length++; - return this.size(); - } - - addFirst(element) { - const node = new Node(element); - if (this.headNode === null) { - this.tailNode = node; - } - node.next = this.headNode; - this.headNode = node; - this.length++; - return this.size(); - } - - // ... (other methods from the original code) - pairwiseSwap() { let current = this.headNode; let prev = null; @@ -85,21 +45,6 @@ class LinkedList { this.tailNode = prev; } } - - get() { - const list = []; - let currentNode = this.headNode; - while (currentNode) { - list.push(currentNode.data); - currentNode = currentNode.next; - } - return list; - } } -// Example usage: -const linkedList = new LinkedList([1, 2, 3, 4, 5]); -console.log("Linked List before pairwise swapping:", linkedList.get()); - -linkedList.pairwiseSwap(); -console.log("Linked List after pairwise swapping:", linkedList.get()); +export { Node, LinkedList };