From a6eb1fc3a7b05816daf24418f7329de6aa09ea4f Mon Sep 17 00:00:00 2001 From: Jason Bongiovanni Date: Mon, 19 Jul 2021 17:23:55 -0400 Subject: [PATCH 1/2] added a display function to linked list algorithms. --- src/data_structures/linked_list.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/data_structures/linked_list.js b/src/data_structures/linked_list.js index b73c852..3fddf64 100644 --- a/src/data_structures/linked_list.js +++ b/src/data_structures/linked_list.js @@ -133,6 +133,20 @@ class LinkedList { node = node.next; } } + + /** + * Displays the full linked list in the terminal when run. + * You can append this function to other functions for an easier to read display. + */ + display(){ + var runner = this.head; + var str = '' + while(runner!=null){ + str += runner.val + " " + runner = runner.next + } + return str + } } /** From f9ac3bc22d8657523a0e385badb3686ad7395a8c Mon Sep 17 00:00:00 2001 From: Jason Bongiovanni Date: Wed, 21 Jul 2021 14:39:25 -0400 Subject: [PATCH 2/2] added remove front node function --- src/data_structures/linked_list.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/data_structures/linked_list.js b/src/data_structures/linked_list.js index 3fddf64..f421ae2 100644 --- a/src/data_structures/linked_list.js +++ b/src/data_structures/linked_list.js @@ -147,8 +147,20 @@ class LinkedList { } return str } + + /** + * removeFront() takes the head off the node, but still prevserves all subsequent nodes. Effectively moves the head to the original head.next. + */ + removeFront(){ + if(!this.head){ + return null + } + this.head = this.head.next + return this.head +} } + /** * A linked list node */