-
Notifications
You must be signed in to change notification settings - Fork 12
Christabel - water #2
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
f86b303
91c6763
e055ca0
77c6156
0d3ac30
6ec8c50
a7529ea
74006fa
738dbb6
808c02a
bfb075f
e53fec5
563d617
4e21ea3
7a5fbe6
c6c11c6
f0eab41
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,161 +17,307 @@ 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!"); | ||
| this.#head = new Node(value, this.#head); | ||
|
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. Why name it |
||
| } | ||
|
|
||
| /* | ||
| 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
+30
to
33
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; | ||
| while (!!current && current.value !== value) { | ||
| current = current.next; | ||
| } | ||
|
|
||
| return !!current; //in js, null is only falsey if coerced into a boolean | ||
| } | ||
|
|
||
| /* | ||
| 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
+45
to
48
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 max = null; | ||
| let current = this.#head; | ||
|
|
||
| while (!!current) { | ||
| if (current.value > max || !max) { | ||
| max = current.value; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
| /* | ||
| 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 min = null; | ||
|
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. Maybe instead assign min to the 1st element in the linked list. Then you don't have to worry about if |
||
| let current = this.#head; | ||
|
|
||
| while (!!current) { | ||
| if (current.value < min || !min) { | ||
| min = current.value; | ||
| } | ||
| current = current.next; | ||
| } | ||
|
|
||
| return min; | ||
| } | ||
|
|
||
| /* | ||
| 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
+83
to
86
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 length = 0; | ||
| let current = this.#head; | ||
|
|
||
| while (!!current) { | ||
| 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
+102
to
105
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 count = 0; | ||
| let current = this.#head; | ||
|
|
||
| while (!!current && count < index) { | ||
| count++; | ||
| current = current.next; | ||
| } | ||
| if (count < index || !this.#head) { | ||
| return null; | ||
| } else { | ||
| return current.value; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| method to print all the values in the linked list | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(n)? Since each console.log call gets added to the call stack? | ||
| */ | ||
| visit() { | ||
|
Comment on lines
+122
to
125
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. 👍 , but the space complexity is O(1) since you only have 1 call to |
||
| throw new Error("This method hasn't been implemented yet!"); | ||
| let current = this.#head; | ||
|
|
||
| while (!!current) { | ||
| console.log(current.value); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| 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
+135
to
138
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 prev = null; | ||
|
|
||
| while (!!current && current.value !== value) { | ||
| prev = current; | ||
| current = current.next; | ||
| } | ||
| if (!!current && current.value === value) { | ||
| if (!!prev) { | ||
| prev.next = current.next; | ||
| } else { | ||
| this.#head = current.next; | ||
| } | ||
| //is garbage collection necessary in JS? No, right? | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| 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
+159
to
162
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 prev = null; | ||
| let current = this.#head; | ||
| let next = current.next; | ||
|
|
||
| while (!!current) { | ||
| current.next = prev; | ||
| prev = current; | ||
| current = next; | ||
| if (!!next) { | ||
| next = next.next; | ||
| } | ||
| } | ||
| this.#head = prev; | ||
| } | ||
|
|
||
|
|
||
| // Advanced Exercises | ||
|
|
||
| /* | ||
| returns the value at the middle element in the singly linked list | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| findMiddleValue() { | ||
|
Comment on lines
+183
to
186
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 slow = this.#head; | ||
| let fast = this.#head; | ||
| while (!!fast && !!fast.next) { //if even number of nodes, exit with !fast else exit with !fast.next | ||
| slow = slow.next; | ||
| fast = fast.next.next; | ||
| } | ||
|
|
||
| return slow.value; | ||
| } | ||
|
|
||
| /* | ||
| find the nth node from the end and return its value | ||
| assume indexing starts at 0 while counting to n | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| findNthFromEnd(n) { | ||
|
Comment on lines
+200
to
203
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 count = 1; //start at one so count can be used as length as well | ||
| let trailing = this.#head; | ||
| let current = this.#head; | ||
| while (!!current && !!current.next) { | ||
| current = current.next; | ||
| count++; | ||
|
|
||
| if (count > n) { | ||
| trailing = trailing.next; | ||
| } | ||
| } | ||
| if (n > count) { | ||
| return null; | ||
| } else { | ||
| return trailing.value; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| checks if the linked list has a cycle. A cycle exists if any node in the | ||
| linked list links to a node already visited. | ||
| returns true if a cycle is found, false otherwise. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| // hasCycle() { | ||
| // let current = this.#head; | ||
| // while (!!current) { | ||
| // current = current.next; | ||
| // if (current == this.#head) { | ||
| // return true | ||
| // } | ||
| // } | ||
|
|
||
| // return false; | ||
| // } | ||
|
|
||
| hasCycle() { | ||
| throw new Error("This method hasn't been implemented yet!"); | ||
| } | ||
| let slow = this.#head; | ||
| let fast = this.#head; | ||
| while (!!fast && !!fast.next) { | ||
| slow = slow.next; | ||
| fast = fast.next.next; | ||
| if (fast == slow) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // Additional Exercises | ||
|
|
||
| /* | ||
| returns the value in the first node | ||
| returns nil if the list is empty | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(1) | ||
| Space Complexity: O(1) | ||
| */ | ||
| getFirst() { | ||
|
Comment on lines
+260
to
263
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. What if the list is empty? |
||
| throw new Error("This method hasn't been implemented yet!"); | ||
| return this.#head.value; | ||
| } | ||
|
|
||
| /* | ||
| method that inserts a given value as a new last node in the linked list | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| addLast(value) { | ||
|
Comment on lines
+269
to
272
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) { | ||
| this.#head = new Node(value, null); | ||
| // could also call this.addFirst(value); | ||
| } else { | ||
| while (!!current.next) { | ||
| current = current.next; | ||
| } | ||
| current.next = new Node(value, null); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| method that returns the value of the last node in the linked list | ||
| returns nil if the linked list is empty | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| getLast() { | ||
|
Comment on lines
+289
to
292
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; | ||
|
|
||
| while (!!current.next) { | ||
| current = current.next; | ||
| } | ||
|
|
||
| return current.value; | ||
| } | ||
|
|
||
| /* | ||
| method to insert a new node with specific data value, assuming the linked | ||
| list is sorted in ascending order | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(1) | ||
| */ | ||
| insertAscending(value) { | ||
|
Comment on lines
+305
to
308
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 || current.value > value) { | ||
| this.#head = new Node(value, this.#head); | ||
| // could also call this.addFirst(value); | ||
| } else { | ||
| while (!!current && !!current.next && current.next.value < value) { | ||
| current = current.next; | ||
| } | ||
|
|
||
| current.next = new Node(value, current.next); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
|
|
||
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.
👍