-
Notifications
You must be signed in to change notification settings - Fork 6
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
Aeron Roemer - Rock #1
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 |
---|---|---|
|
@@ -13,106 +13,173 @@ class LinkedList { | |
constructor() { | ||
// The # before the variable name indicates | ||
// a private variable. | ||
this.#head = null; | ||
this.head = null; | ||
} | ||
|
||
/* | ||
Method to retrieve the value in the first node. | ||
returns null if the list is empty. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O1 | ||
Space Complexity: O1 | ||
*/ | ||
getFirst() { | ||
throw new Error('This method has\'t been implemented yet!') | ||
return this.head | ||
} | ||
|
||
/* | ||
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: O1 | ||
Space Complexity: O1 | ||
*/ | ||
addFirst(value) { | ||
Comment on lines
+32
to
35
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!"); | ||
this.head = new Node(value, this.head) | ||
} | ||
|
||
/* | ||
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: On | ||
Space Complexity: O1 | ||
*/ | ||
search(value) { | ||
Comment on lines
+42
to
45
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!"); | ||
if (!this.head) return false; | ||
|
||
let currentItem = this.head | ||
|
||
while (currentItem){ | ||
if (currentItem.value == value) return true; | ||
currentItem = currentItem.next | ||
} | ||
return false | ||
} | ||
|
||
/* | ||
method that returns the length of the singly linked list | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: On | ||
Space Complexity: O1 | ||
*/ | ||
length() { | ||
Comment on lines
+59
to
62
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!"); | ||
if (!this.head) return 0; | ||
|
||
let counter = 0 | ||
let currentItem = this.head | ||
|
||
while (currentItem){ | ||
counter ++ | ||
currentItem = currentItem.next | ||
} | ||
return counter; | ||
} | ||
|
||
/* | ||
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: On | ||
Space Complexity: O1 | ||
*/ | ||
getAtIndex(index) { | ||
Comment on lines
+79
to
82
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!"); | ||
if (!this.head) return null; | ||
|
||
let counter = 0 | ||
let currentItem = this.head | ||
|
||
while (currentItem){ | ||
if (counter == index) return currentItem.value; | ||
counter ++ | ||
currentItem = currentItem.next | ||
} | ||
} | ||
|
||
/* | ||
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: On | ||
Space Complexity: O1 | ||
*/ | ||
getLast() { | ||
Comment on lines
+98
to
101
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!"); | ||
if (!this.head) return null; | ||
|
||
let currentItem = this.head | ||
|
||
while (currentItem.next){ | ||
currentItem = currentItem.next | ||
} | ||
return currentItem.value; | ||
} | ||
|
||
/* | ||
method that inserts a given value as a new last node in the linked list | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: on | ||
Space Complexity: O1 | ||
*/ | ||
addLast(value) { | ||
Comment on lines
+114
to
117
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!"); | ||
if (!this.head) { | ||
this.addFirst(value) | ||
return | ||
}; | ||
let currentItem = this.head | ||
while (currentItem.next){ | ||
currentItem = currentItem.next | ||
} | ||
currentItem.next = new Node(value) | ||
return this.head; | ||
} | ||
|
||
/* | ||
method to return the max value in the linked list | ||
returns the data value and not the node | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: On | ||
Space Complexity: O1 | ||
*/ | ||
findMax() { | ||
Comment on lines
+133
to
136
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!"); | ||
if (!this.head) return null; | ||
|
||
let max = 0 | ||
let currentItem = this.head | ||
|
||
while (currentItem){ | ||
if (max < currentItem.value){ | ||
max = currentItem.value; | ||
} | ||
currentItem = currentItem.next | ||
} | ||
return max | ||
} | ||
|
||
/* | ||
method to delete the first node found with specified value | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: On | ||
Space Complexity: O1 | ||
*/ | ||
delete(value) { | ||
Comment on lines
+153
to
156
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 has\'t been implemented yet!') | ||
} | ||
if (!this.head) return null; | ||
if (this.head.value == value) { | ||
this.head = this.head.next | ||
return | ||
} | ||
let currentItem = this.head; | ||
let previous | ||
|
||
while (currentItem != null){ | ||
if (currentItem.value == value){ | ||
previous.next = currentItem.next | ||
} | ||
previous = currentItem | ||
currentItem = currentItem.next | ||
} | ||
} | ||
/* | ||
method to print all the values in the linked list | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: On | ||
Space Complexity: O1 | ||
*/ | ||
visit() { | ||
Comment on lines
+175
to
178
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. 👍 |
||
const helper_list = [] | ||
current = this.#head; | ||
|
||
if (this.head){ | ||
current = this.head; | ||
} | ||
while (current) { | ||
helper_list.push(current.value); | ||
current = current.next; | ||
|
@@ -125,11 +192,32 @@ class LinkedList { | |
/* | ||
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: On | ||
Space Complexity: O1 | ||
*/ | ||
reverse() { | ||
throw new Error("This method hasn't been implemented yet!"); | ||
if (!this.head) return; | ||
let reversed | ||
let currentItem = this.head | ||
let nextItem | ||
while (currentItem){ | ||
nextItem = currentItem.next; | ||
currentItem.next = reversed; | ||
reversed = currentItem | ||
currentItem = nextItem | ||
} | ||
this.head = reversed | ||
} | ||
|
||
|
||
while (current) { | ||
// hold the next node temporarily | ||
nextTemp = current.next; | ||
// change current to point at the previous node | ||
current.next = previous; | ||
// increment previous and current each by one | ||
previous = current; | ||
current = nextTemp; | ||
} | ||
Comment on lines
+213
to
221
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. I'm not sure what purpose this code serves. |
||
|
||
// Advanced Exercises | ||
|
@@ -169,15 +257,15 @@ class LinkedList { | |
Assumes the linked list has at least one node | ||
*/ | ||
createCycle() { | ||
if (!this.#head) return; // don't do anything if the linked list is empty | ||
if (!this.head) return; // don't do anything if the linked list is empty | ||
|
||
// navigate to the last node | ||
let current = this.#head; | ||
let current = this.head; | ||
while (current.next) { | ||
current = current.next; | ||
} | ||
|
||
current.next = this.#head; | ||
current.next = this.head; | ||
} | ||
|
||
end | ||
|
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.
👍