diff --git a/lib/linked-list.js b/lib/linked-list.js index 09800dd..c4ee08f 100644 --- a/lib/linked-list.js +++ b/lib/linked-list.js @@ -17,87 +17,180 @@ 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 - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(1) */ 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 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 @@ -142,7 +235,9 @@ 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 } /*