-
Notifications
You must be signed in to change notification settings - Fork 12
Kayla - 🌊 (SORRY I'M LATE!!!) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little simpler
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| 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) { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+34
to
37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+56
to
59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+77
to
80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+97
to
100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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) { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+116
to
119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+132
to
135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 , since you are traversing the list twice (once for the ll and the other time for the array) it's O(n + n) => O(n) in time complexity. Since you are creating an array the space complexity is O(n). |
||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+136
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| method to delete the first node found with specified value | ||||||||||||||||||||||||||||||||||
| Time Complexity: ? | ||||||||||||||||||||||||||||||||||
| Space Complexity: ? | ||||||||||||||||||||||||||||||||||
| Time Complexity: O(n) | ||||||||||||||||||||||||||||||||||
| Space Complexity: O(1) | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| delete(value) { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+148
to
151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+176
to
179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| 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() { | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||||||||||||||
| throw new Error("This method hasn't been implemented yet!"); | ||||||||||||||||||||||||||||||||||
| let current = this.#head; | ||||||||||||||||||||||||||||||||||
| if (current == null) return null | ||||||||||||||||||||||||||||||||||
| return current.value | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍