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

Linked List Assignment #3

Open
wants to merge 2 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
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{

Choose a reason for hiding this comment

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

This could probably be added to .gitignore

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
175 changes: 130 additions & 45 deletions lib/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Node {
}
// Defines the singly linked list
class LinkedList {
// keep the head private. Not accessible outside this class
// keep the #head private. Not accessible outside this class
// note that this language feature is only available from Node 12 on
// #head;
#head
constructor() {
// The # before the variable name indicates
// a private variable.
Expand All @@ -19,148 +20,232 @@ class LinkedList {
/*
Method to retrieve the value in the first node.
returns null if the list is empty.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(1)
Space Complexity: O(1)
*/
getFirst() {
Comment on lines +23 to 26

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)? null: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: O(1)
Space Complexity: O(1)
*/
addFirst(value) {
Comment on lines +33 to 36

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 newNode = new Node(value)
if (!this.#head) {
this.#head = newNode
} else {
newNode.next = this.#head
this.#head = newNode
}
}

/*
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 +49 to 52

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) {
if (current.value === value) {
return true;
}
current = current.next;
}
return false;
}

/*
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 +65 to 68

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 ++
current = current.next
if (current == this.#head){
return count
}
}
return count
}

/*
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 +85 to 88

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) {
if (count === index) {
return current.value
} else {
count ++
current = current.next
}
}
return 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() {
throw new Error("This method hasn't been implemented yet!");
let current = this.#head;
while (current.next) {
current = current.next
}
return current.value
}
Comment on lines +105 to 114

Choose a reason for hiding this comment

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

You also need to check to see if the list is empty


/*
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 +118 to 121

Choose a reason for hiding this comment

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

👍 Just a note if you maintain a tail reference this can be done in O(1) time.

throw new Error("This method hasn't been implemented yet!");
if (!this.#head) {
this.#head = new Node(value)
} else {
let current = this.#head;
while (current.next) {
current = current.next
}
current.next = new Node(value)
}
}

/*
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 +136 to 139

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 = this.#head.value
let current = this.#head;
while (current) {
(current.value>max)? (max = current.value):
current = current.next
}
return max;
}

/*
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 +154 to 157

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!')
(this.#head?.value === value)?(this.#head = this.#head.next):false;

Choose a reason for hiding this comment

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

This line looks a little awkward with the assignment in the middle of the ternary.

let current = this.#head
while (current) {
(current.next?.value === value)? (current.next = current.next.next):
current = current.next
}
}


/*
method to print all the values in the linked list
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(1)
*/
visit() {
Comment on lines +169 to 172

Choose a reason for hiding this comment

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

👍 Space complexity of O(n) because you're building a list.

const helper_list = []
current = this.#head;

let current = this.#head;
while (current) {
helper_list.push(current.value);
current = current.next;
}

console.log(helper_list.toString());
console.log(helper_list);
}


/*
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 +187 to 190

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
let next
while (current) {
if (current===this.#head) {
prev = current
current = current.next
} else {
next = current.next
current.next = prev
prev = current
current = 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 +211 to 214

Choose a reason for hiding this comment

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

👍 Good reuse of existing methods and arithmetic.

throw new Error("This method hasn't been implemented yet!");
let len = this.length()
const evenCheck = len%2
return evenCheck ? this.getAtIndex(Math.floor(len/2)):(this.getAtIndex((len/2)))

}

/*
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 +224 to 227

Choose a reason for hiding this comment

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

👍 Good reuse of existing methods and arithmetic.

throw new Error("This method hasn't been implemented yet!");
return(this.getAtIndex(this.length()-n))
}

/*
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() {
Comment on lines +235 to 238

Choose a reason for hiding this comment

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

What if the loop isn't back to the `head? What if it loops back to the 3rd node in the list?

throw new Error("This method hasn't been implemented yet!");
let current = this.#head;
let count = 0;
while (current) {
count ++
current = current.next
if (current.next == this.#head){
return true
}
}
return false
}

/*
Expand Down
Loading