-
Notifications
You must be signed in to change notification settings - Fork 23
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
finished #1
base: main
Are you sure you want to change the base?
finished #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,157 @@ | ||
const { nums, words } = require("./data/data.js"); | ||
|
||
|
||
class Node { | ||
constructor(data) { | ||
this.data = data; | ||
this.next = null; | ||
} | ||
}; | ||
|
||
|
||
class LinkedList { | ||
constructor(head = null) { | ||
this.head = head | ||
}; | ||
|
||
size() { | ||
let count = 0; | ||
let currentNode = this.head; | ||
while(currentNode) { | ||
count++; | ||
currentNode = currentNode.next; | ||
} | ||
return count; | ||
}; | ||
|
||
search(value) { | ||
let currentNode = this.head; | ||
while(currentNode) { | ||
if (currentNode.data === value) { | ||
return currentNode; | ||
}; | ||
currentNode = currentNode.next; | ||
}; | ||
}; | ||
|
||
clear() { | ||
this.head = null; | ||
} | ||
|
||
getFirst() { | ||
return this.head; | ||
} | ||
|
||
getLast() { | ||
let currentNode = this.head | ||
while(currentNode.next != null){ | ||
currentNode = currentNode.next; | ||
} | ||
return currentNode; | ||
} | ||
|
||
insert(value) { | ||
let newNode = new Node(value); | ||
if(!this.head){ | ||
this.head = newNode; | ||
} else { | ||
newNode.next = this.head; | ||
this.head = newNode; | ||
} | ||
} | ||
|
||
|
||
delete(position) { | ||
if (position <= 0 || position > this.size()) { | ||
return; | ||
} | ||
if (position === 1) { | ||
this.head = this.head.next; | ||
return; | ||
} | ||
let currentNode = this.head; | ||
let count = 1; | ||
while (count < position - 1) { | ||
currentNode = currentNode.next; | ||
count++; | ||
} | ||
currentNode.next = currentNode.next.next; | ||
}; | ||
|
||
isEmpty() { | ||
let count = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a way to write this in one line and O(n) run time |
||
let currentNode = this.head; | ||
while(currentNode) { | ||
count++; | ||
currentNode = currentNode.next; | ||
} | ||
if(count === 0){ | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
getKth(position) { | ||
let count = 0; | ||
let currentNode = this.head; | ||
while(currentNode != null) { | ||
count ++ | ||
if(count === position){ | ||
return currentNode | ||
} | ||
currentNode = currentNode.next | ||
} | ||
} | ||
|
||
getKthToLast(kth) { | ||
let slow = this.head; | ||
let fast = this.head.next; | ||
for (let i = 0; i < kth; i++) { | ||
fast = fast.next | ||
} | ||
while (fast != null) { | ||
slow = slow.next; | ||
fast = fast.next; | ||
} | ||
return slow; | ||
} | ||
|
||
toArray() { | ||
let currentNode = this.head | ||
let arr = [] | ||
while(currentNode) { | ||
arr.push(currentNode.data) | ||
currentNode = currentNode.next | ||
} | ||
return arr | ||
} | ||
|
||
containsDuplicates() { | ||
let visited = new Set(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a set you could compare the length of the set to the size of the linked list for shorter, cleaner code |
||
let currentNode = this.head; | ||
while (currentNode) { | ||
if (visited.has(currentNode.data)) { | ||
return true; | ||
} | ||
visited.add(currentNode.data); | ||
currentNode = currentNode.next; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
let numsHead = new Node(nums[0]) | ||
let list = new LinkedList(numsHead); | ||
for (let i = 1; i < nums.length; i++) { | ||
let currentNode = new Node(nums[i]); | ||
numsHead.next = currentNode; | ||
numsHead = currentNode; | ||
} | ||
|
||
console.log(list.getKthToLast(8)) | ||
|
||
|
||
module.exports = { | ||
Node, | ||
LinkedList, | ||
|
There was a problem hiding this comment.
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)?