Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
166 changes: 127 additions & 39 deletions lib/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Comment on lines +22 to 25

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

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

/*
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

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!");
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

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!");
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

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!");
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

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!");
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

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!");
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

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!");
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

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

Choose a reason for hiding this comment

The 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;
Expand All @@ -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

Choose a reason for hiding this comment

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

I'm not sure what purpose this code serves.


// Advanced Exercises
Expand Down Expand Up @@ -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
Expand Down
Loading