Skip to content
Open
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
155 changes: 125 additions & 30 deletions lib/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,180 @@ 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!");
const addNode = new Node(value) // create Node object
addNode.next = this.#head // pointer 'next' is assigned to current head pointer
this.#head = addNode; //Head is now equal to new node
let current = this.#head //better name for head
return current
Comment on lines +24 to +28

Choose a reason for hiding this comment

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

A little simpler

Suggested change
const addNode = new Node(value) // create Node object
addNode.next = this.#head // pointer 'next' is assigned to current head pointer
this.#head = addNode; //Head is now equal to new node
let current = this.#head //better name for head
return current
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: O(n)
Space Complexity: O(1)
*/
search(value) {
Comment on lines +34 to 37

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

while(current != null) {
if(current.value === value) {
return true;
}else {
current = current.next;
}
}
return false;
}

/*
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 +56 to 59

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 maxNum = 0

let current = this.#head
if (current == null) return null

while(current != null) {
if(current.value > maxNum) {
maxNum = current.value
}else {
current = current.next
}
}
return maxNum
}
/*
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() {
Comment on lines +77 to 80

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 == null) return null;

let minNum = current.value;
while(current != null) {
if(current.value < minNum) {
minNum = current.value
}else {
current = current.next
}
}
return minNum
}

/*
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 +97 to 100

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 == null) return 0;
let length = 0

while(current != null) {
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 +116 to 119

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 count = 0
if (current == null) return null
while(count != null && count < index) {
count++
current = current.next
}
return current.value
}

/*
method to print all the values in the linked list
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n) * O(k) -- i don't know! You have to first traverse the linked list and THEN an array
Space Complexity: ???
*/
visit() {
Comment on lines +132 to 135

Choose a reason for hiding this comment

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

👍 , since you are traversing the list twice (once for the ll and the other time for the array) it's O(n + n) => O(n) in time complexity.

Since you are creating an array the space complexity is O(n).

throw new Error("This method hasn't been implemented yet!");
let current = this.#head
if (current == null) return null;
let printJob = [];

while(current != null) {
printJob.push(current.value);
current = current.next;
}
console.log(printJob.join(' ')); //print array as string
Comment on lines +136 to +144

Choose a reason for hiding this comment

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

Suggested change
let current = this.#head
if (current == null) return null;
let printJob = [];
while(current != null) {
printJob.push(current.value);
current = current.next;
}
console.log(printJob.join(' ')); //print array as string
let current = this.#head
if (current == null) return null;
while (current != null) {
console.log(current.value + ' ');
current = current.next;
}

}
/*
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 +148 to 151

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 previous = null;

if (current === null) return null; // empty list returns null

if (current.value === value) {
this.#head = current.next; //in case first value is value to be deleted
return
}

while (current != null) {
if (current.value == value) {
previous.next = current.next;
return
}
previous = current
current = current.next;
}
return "Value not found!";
}

/*
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 +176 to 179

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 previous = null;
let next = null;

if (current === null) return null;

while(current != null) {
next = current.next; // reassign current.next before reassigning it to previous
current.next = previous;
previous = current;
current = next;
}
this.#head = previous
return
}

// Advanced Exercises
Expand Down Expand Up @@ -142,7 +235,9 @@ class LinkedList {
Space Complexity: ?
*/
getFirst() {

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 == null) return null
return current.value
}

/*
Expand Down