Add comments to Linked List code.

This commit is contained in:
Oleksii Trekhleb 2018-07-30 15:35:28 +03:00
parent 5105898aa7
commit 97926b1243

View File

@ -128,6 +128,7 @@ export default class LinkedList {
*/ */
deleteTail() { deleteTail() {
const deletedTail = this.tail; const deletedTail = this.tail;
if (this.head === this.tail) { if (this.head === this.tail) {
// There is only one node in linked list. // There is only one node in linked list.
this.head = null; this.head = null;
@ -135,6 +136,9 @@ export default class LinkedList {
return deletedTail; return deletedTail;
} }
// If there are many nodes in linked list...
// Rewind to the last node and delete "next" link for the node before the last one. // Rewind to the last node and delete "next" link for the node before the last one.
let currentNode = this.head; let currentNode = this.head;
while (currentNode.next) { while (currentNode.next) {