From 21d54e4774c2f11cf8b50f5e19de46b411edd8e6 Mon Sep 17 00:00:00 2001 From: Omid Rajabi Date: Tue, 1 Feb 2022 08:53:45 -0500 Subject: [PATCH] added linkedlist swap --- src/data-structures/linked-list/LinkedList.js | 57 +++++++++++++++++++ .../linked-list/LinkedListNode.js | 8 +++ src/data-structures/linked-list/README.md | 45 +++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index ba7d0e3e..c7e075b0 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -2,6 +2,8 @@ import LinkedListNode from './LinkedListNode'; import Comparator from '../../utils/comparator/Comparator'; export default class LinkedList { + head: any; + tail: any; /** * @param {Function} [comparatorFunction] */ @@ -269,4 +271,59 @@ export default class LinkedList { return this; } + + /** + * @param {*} value + * @return {LinkedList} + */ + swap(data1, data2) { + let node1Prev = null; + let node2Prev = null; + let node1 = this.head; + let node2 = this.head; + + // elements are same, no swap to be made + if (data1 === data2) { + return; + } + + while (node1 !== null) { + if (node1.data === data1) { + break; + } + node1Prev = node1; + node1 = node1.next; + } + + while (node2 !== null) { + if (node2.data === data2) { + break; + } + node2Prev = node2; + node2 = node2.next; + } + + // swap not possible, one or more element not in list + if (node1 === null || node2 === null) { + return; + } + + if (node1Prev === null) { + this.head = node2; + } else { + node1Prev.setNextNode(node2); + } + + if (node2Prev === null) { + this.head = node1; + } else { + node2Prev.setNextNode(node1); + } + + let temp = node1.next; + node1.setNextNode(node2.next); + node2.setNextNode(temp); + } + + } diff --git a/src/data-structures/linked-list/LinkedListNode.js b/src/data-structures/linked-list/LinkedListNode.js index d136e863..d195aa76 100644 --- a/src/data-structures/linked-list/LinkedListNode.js +++ b/src/data-structures/linked-list/LinkedListNode.js @@ -7,4 +7,12 @@ export default class LinkedListNode { toString(callback) { return callback ? callback(this.value) : `${this.value}`; } + + setNextNode(node) { + if (node instanceof LinkedListNode || node === null) { + this.next = node; + } else { + throw new Error('Next node must be a member of the Node class.'); + } + } } diff --git a/src/data-structures/linked-list/README.md b/src/data-structures/linked-list/README.md index d4b9fa53..a7adf73c 100644 --- a/src/data-structures/linked-list/README.md +++ b/src/data-structures/linked-list/README.md @@ -149,6 +149,51 @@ ReverseTraversal(head, tail) end ReverseTraversal ``` +### Swap + +```text +Swap(head, data1, data2) + Pre: head is the head node in the list + data1 and data2 are valid values + Post: the 2 items in the list have been swapped + if data1 === data2 + end Swap + node1Prev ← null + node2Prev ← null + node1 ← head + node2 ← head + while node1 != null + if node1.data === data1 + break + node1Prev ← node1 + node1 ← node1.next + end while + while node2 != null + if node2.data === data2 + break + node2Prev ← node2 + node2 ← node2.next + end while + if !node1 or !node2 + end + endif + if !node1Prev + head ← node2 + else + node1Prev.next ← node2 + endif + if !node2Prev + head ← node1 + else + node2Prev.next ← node1 + endif + let temp ← node1.next + node1.next ← node2.next + node2.next ← temp + endSwap + +``` + ## Complexities ### Time Complexity