enh(Javascript): add rotation feature in singly link list. (#530)
parent
cf7a713fd9
commit
ac25147171
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue