Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 195 additions & 49 deletions lib/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment on lines +20 to 23

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why name it #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: O(n)
Space Complexity: O(1)
*/
search(value) {
Comment on lines +30 to 33

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The 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 min is null.

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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 console.log on the call stack at any one time.

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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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);
}
}

/*
Expand Down
Loading