From ac25147171544e7fa8027d36309bbaf4ca7d8d41 Mon Sep 17 00:00:00 2001 From: atulll <39869656+underager@users.noreply.github.com> Date: Fri, 8 Oct 2021 18:41:18 +0530 Subject: [PATCH] enh(Javascript): add rotation feature in singly link list. (#530) --- .../JavaScript/src/linked-lists/singly.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/algorithms/JavaScript/src/linked-lists/singly.js b/algorithms/JavaScript/src/linked-lists/singly.js index f6641e25..da192b3b 100644 --- a/algorithms/JavaScript/src/linked-lists/singly.js +++ b/algorithms/JavaScript/src/linked-lists/singly.js @@ -152,6 +152,28 @@ class SinglyLinkedList { return returnNode; } + rotate() { + if (!this.head) { + return; + } + + let currentNode = this.head; + let previousNode = null; + let tempNextRefNode; + + while (currentNode) { + tempNextRefNode = currentNode.next; + currentNode.next = previousNode; + + previousNode = currentNode; + currentNode = tempNextRefNode; + } + + this.head = previousNode; + + return this; + } + printList() { // if head is null then list is empty if (this.head == null) { @@ -185,3 +207,7 @@ array.printList(); array.removeFromEnd(); array.printList(); + +array.rotate(); + +array.printList();