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 complete #4

Open
wants to merge 1 commit 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
102 changes: 102 additions & 0 deletions solution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,107 @@
const { nums, words } = require("./data/data.js");
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

//create nodes
const firstNode = new Node(1);
const secondNode = new Node(2);
firstNode.next = secondNode;
//console.log(firstNode);

//linked list blueprint
class LinkedList {
constructor(head = null) {
this.head = head;
}
size() {
let count = 0;
let node = this.head;
while (node) {
count++;
node = node.next;
}
return count;
}
search(key) {
let node = this.head;
while (node !== null && node.data !== key) {
node = node.next;
}
return node;
}
clear() {
this.head = null;
}
getLast() {
let node = this.head;
while (node.next) {
node = node.next;
}
return node;
}
insert(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
delete(data) {
let node = this.head;
let counter = 0;
while (node.data !== data && node.next) {
counter++;
node = node.next;
}
let foundNode = node;
node = this.head;
for (let i = 1; i < counter; i++) {
node = node.next;
}
//the next node has the value of the next node after foundnode
node.next = foundNode.next;
}
getFirst() {
return this.head;
}
getKth(k) {
let currentNode = this.head;
for (let i = 0; i < k; i++) {
if (currentNode.next) {
} else {
return undefined;
}
}
return currentNode;
}
getKthToLast(k){
let getSize = this.size();
let node = this.head;
for (let i = 1; i < getSize - k; i++){
node = node.next;
}
return node;
}
isEmpty(){
return !this.head;
}
toArray(){
let arr = [];
let item = this.head;
while (item){
arr.push(item.data);
item = item.next
}
return arr;
}
containsDuplicates(){
let arr = this.toArray();
let unique = [...new Set(arr)];
return arr.length !== unique.length;
}
}
module.exports = {
Node,
LinkedList,
Expand Down