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();