Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pair wise swap #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pojha1401 wants to merge 2 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from pojha1401:pairWiseSwap
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
updated pairwiseswap
  • Loading branch information
pojha1401 committed Oct 12, 2023
commit 4882828ab12f0e2820db222e5d2bc2f8624889bc
59 changes: 2 additions & 57 deletions Data-Structures/Linked-List/pairWiseSwap.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 };

AltStyle によって変換されたページ (->オリジナル) /