From c48a8646cff3124c63d97142adaeee8aaa55e110 Mon Sep 17 00:00:00 2001 From: Kayla Johnson Date: Thu, 25 Mar 2021 19:53:34 -0700 Subject: [PATCH 1/2] FINALLY GOT DELETE TO WORKgit add . --- lib/linked-list.js | 133 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 109 insertions(+), 24 deletions(-) diff --git a/lib/linked-list.js b/lib/linked-list.js index 09800dd..33bc9f1 100644 --- a/lib/linked-list.js +++ b/lib/linked-list.js @@ -17,69 +17,131 @@ class LinkedList { /* method to add a new node with the specific data value in the linked list insert the new node at the beginning of the linked list - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(1) + Space Complexity: O(1) */ addFirst(value) { - throw new Error("This method hasn't been implemented yet!"); + const addNode = new Node(value) // create Node object + addNode.next = this.#head // pointer 'next' is assigned to current head pointer + this.#head = addNode; //Head is now equal to new node + let current = this.#head //better name for head + return current } /* method to find if the linked list contains a node with specified value returns true if found, false otherwise - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ search(value) { - throw new Error("This method hasn't been implemented yet!"); + + let current = this.#head + if (!current) return false + if(current.value === value) return true + + while(current != null) { + if(current.value === value) { + return true; + }else { + current = current.next; + } } + return false; +} /* method to return the max value in the linked list returns the data value and not the node - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ findMax() { - throw new Error("This method hasn't been implemented yet!"); + let maxNum = 0 + + let current = this.#head + if (current == null) return null + + while(current != null) { + if(current.value > maxNum) { + maxNum = current.value + }else { + current = current.next + } + } + return maxNum } /* method to return the min value in the linked list returns the data value and not the node - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ findMin() { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head + if (current == null) return null; + + let minNum = current.value; + while(current != null) { + if(current.value < minNum) { + minNum = current.value + }else { + current = current.next + } + } + return minNum } /* method that returns the length of the singly linked list - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ length() { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head + if (current == null) return 0; + let length = 0 + + while(current != null) { + length ++; + current = current.next; + } + return length } /* method that returns the value at a given index in the linked list index count starts at 0 returns nil if there are fewer nodes in the linked list than the index value - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ getAtIndex(index) { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head + let count = 0 + if (current == null) return null + while(count != null && count < index) { + count++ + current = current.next + } + return current.value } /* method to print all the values in the linked list - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) * O(k) -- i don't know! You have to first traverse the linked list and THEN an array + Space Complexity: ??? */ visit() { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head + if (current == null) return null; + let printJob = []; + + while(current != null) { + printJob.push(current.value); + current = current.next; + } + console.log(printJob.join(' ')); //print array as string } /* method to delete the first node found with specified value @@ -87,8 +149,28 @@ class LinkedList { Space Complexity: ? */ delete(value) { - throw new Error("This method hasn't been implemented yet!"); - } + let current = this.#head; + let previous = null; + + if (current === null) return null; // empty list returns null + + if (current.value === value) { + this.#head = current.next; //in case first value is value to be deleted + return + } + + while (current != null) { + if (current.value == value) { + previous.next = current.next; + return + } + previous = current + current = current.next; + } + return "Value not found!"; + } + + /* method to reverse the singly linked list @@ -142,7 +224,10 @@ class LinkedList { Space Complexity: ? */ getFirst() { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head; + if (current == null) return null + + return current.value } /* From 71004c9c7fcab220b899080a9e1cce9993f711d9 Mon Sep 17 00:00:00 2001 From: Kayla Johnson Date: Thu, 25 Mar 2021 20:22:48 -0700 Subject: [PATCH 2/2] Reversed method working --- lib/linked-list.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/linked-list.js b/lib/linked-list.js index 33bc9f1..c4ee08f 100644 --- a/lib/linked-list.js +++ b/lib/linked-list.js @@ -145,8 +145,8 @@ class LinkedList { } /* method to delete the first node found with specified value - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ delete(value) { let current = this.#head; @@ -170,16 +170,27 @@ class LinkedList { return "Value not found!"; } - - /* method to reverse the singly linked list note: the nodes should be moved and not just the values in the nodes - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ reverse() { - throw new Error("This method hasn't been implemented yet!"); + let current = this.#head; + let previous = null; + let next = null; + + if (current === null) return null; + + while(current != null) { + next = current.next; // reassign current.next before reassigning it to previous + current.next = previous; + previous = current; + current = next; + } + this.#head = previous + return } // Advanced Exercises @@ -226,7 +237,6 @@ class LinkedList { getFirst() { let current = this.#head; if (current == null) return null - return current.value }