File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
lib/data-structures/linked-lists Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 11const Node = require ( './node' ) ;
22
3+ /**
4+ * Singly linked list
5+ */
36class LinkedList {
47 constructor ( ) {
58 this . root = null ;
69 }
710
11+ /**
12+ * Adds element to the end of the list (tail)
13+ * Runtime: O(n)
14+ * @param {any } value
15+ */
816 addLast ( value ) { // similar Array.push
917 const node = new Node ( value ) ;
1018
@@ -19,6 +27,10 @@ class LinkedList {
1927 }
2028 }
2129
30+ /**
31+ * Removes element from the start of the list (head/root)
32+ * Runtime: O(1)
33+ */
2234 removeFirst ( ) { // similar Array.shift
2335 const first = this . root ;
2436
@@ -28,13 +40,23 @@ class LinkedList {
2840 }
2941 }
3042
43+ /**
44+ * Adds element to the begining of the list
45+ * Runtime: O(1)
46+ * @param {any } value
47+ */
3148 addFirst ( value ) { // similar to Array.unshift
3249 const node = new Node ( value ) ;
3350 node . next = this . root ;
3451 this . root = node ;
3552 }
3653
37- removeLast ( ) { // similar to Array.pop
54+ /**
55+ * Removes element to the end of the list
56+ * similar to Array.pop
57+ * Runtime: O(n)
58+ */
59+ removeLast ( ) {
3860 let current = this . root ;
3961 let target ;
4062
Original file line number Diff line number Diff line change 1+ /**
2+ * Simple node
3+ */
14class Node {
25 constructor ( value ) {
36 this . value = value ;
You can’t perform that action at this time.
0 commit comments