From 97926b124317c3b6d9dc651a1f7d38326c250671 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 30 Jul 2018 15:35:28 +0300 Subject: [PATCH] Add comments to Linked List code. --- src/data-structures/linked-list/LinkedList.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 4d6b9e26..ce5a2544 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -128,6 +128,7 @@ export default class LinkedList { */ deleteTail() { const deletedTail = this.tail; + if (this.head === this.tail) { // There is only one node in linked list. this.head = null; @@ -135,6 +136,9 @@ export default class LinkedList { 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. let currentNode = this.head; while (currentNode.next) {