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

Lab completed. All tests passed. #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
160 changes: 160 additions & 0 deletions solution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,165 @@
const { nums, words } = require("./data/data.js");

class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
}
// 1. Create a method called `insert` that takes in a `data` argument and creates a new `Node` with the data and adds it to the front of the list.
insert(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}

size() {
let count = 0;
let currentNode = this.head;
while (currentNode) {
count++;
currentNode = currentNode.next;
}
return count;
}
Comment on lines +21 to +29

Choose a reason for hiding this comment

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

Can you think of a way to have this function run in O(1) instead of O(n)?


getFirst() {
return this.head;
}

getLast() {
let currentNode = this.head;
while (currentNode) {
if (currentNode.next === null) {
return currentNode;
}
currentNode = currentNode.next;
}
}

search(data) {
let currentNode = this.head;
while (currentNode) {
if (currentNode.data === data) {
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}

// getkth(k) {
// let currentNode = this.head;
// let count = 1;
// while(currentNode) {
// if(count === k) {
// return currentNode;
// }
// currentNode = currentNode.next;
// count++;
// }
// return null;
// }

getKth(k) {
let currentNode = this.head;
let count = 1;
while (currentNode) {
if (count === k) {
return currentNode;
}
currentNode = currentNode.next;
count++;
}
return null;
}

getKthToLast(k) {
let currentNode = this.head;
let count = 1;
while (currentNode) {
if (count === this.size() - k) {
return currentNode;
}
currentNode = currentNode.next;
count++;
}
return null;
}

isEmpty() {
if (this.head === null) {
return true;
} else {
return false;
}
Comment on lines +96 to +100

Choose a reason for hiding this comment

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

Suggested change
if (this.head === null) {
return true;
} else {
return false;
}
!this.head

}

clear() {
this.head = null; //clear the head and tail and set the length to 0 to clear the list
}

toArray() {
let currentNode = this.head;
let array = [];
while (currentNode) {
array.push(currentNode.data);
currentNode = currentNode.next;
}
return array;
}

// delete(position) {
// let currentNode = this.head;
// let count = 1;
// while (currentNode) {
// if (count + 1 === position) {
// let temp = currentNode.next.next;
// currentNode.next = temp;
// return this.head;
// }
// currentNode = currentNode.next;
// count++;
// }
// return currentNode
// }

delete(data) {
let currentNode = this.head;
let previousNode = null;
while (currentNode) {
if (currentNode.data === data) {
if (previousNode) {
previousNode.next = currentNode.next;
} else {
this.head = currentNode.next;
}
return;
}
previousNode = currentNode;
currentNode = currentNode.next;
}
}

containsDuplicates() {
let currentNode = this.head;
let array = [];
while (currentNode) {
if (array.includes(currentNode.data)) {
return true;
}
array.push(currentNode.data);
currentNode = currentNode.next;
}
return false;
}
}
Comment on lines +149 to +161

Choose a reason for hiding this comment

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

Can you think of a shorter, cleaner and more efficient way to write this function using this.toArray and a set?


module.exports = {
Node,
LinkedList,
Expand Down