enh(Javascript): add rotation feature in singly link list. (#530)

pull/613/head
atulll 2021-10-08 18:41:18 +05:30 committed by GitHub
parent cf7a713fd9
commit ac25147171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 0 deletions

View File

@ -152,6 +152,28 @@ class SinglyLinkedList {
return returnNode; 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() { printList() {
// if head is null then list is empty // if head is null then list is empty
if (this.head == null) { if (this.head == null) {
@ -185,3 +207,7 @@ array.printList();
array.removeFromEnd(); array.removeFromEnd();
array.printList(); array.printList();
array.rotate();
array.printList();