Skip to content
Merged
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
56 changes: 31 additions & 25 deletions src/_DataStructures_/LinkedList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ class Node {
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}

addAtBeginning(element) {
this.head = new Node(element, this.head);
if (!this.tail) {
this.tail = this.head;
}
this.size += 1;
return this.head;
}

addAtEnd(element) {
const node = new Node(element, null);

if (!this.head) {
this.head = node;
} else {
let address = this.head;
while (address.next) {
address = address.next;
}
address.next = node;
return this.addAtBeginning(element);
}
const node = new Node(element, null);
this.tail.next = node;
this.tail = node;
this.size += 1;
return node;
}

removeFromBeginning() {
if (!this.head) {
return null;
}
if (this.head.next === null) {
this.tail = this.head;
}
const node = this.head;
this.head = this.head.next;
this.size -= 1;
return node;
}

Expand All @@ -47,8 +55,11 @@ class LinkedList {
address = address.next;
}

const node = address.next;
address.next = null;
this.tail = address;

const node = this.tail.next;
this.tail.next = null;
this.size -= 1;
return node;
}

Expand All @@ -63,11 +74,7 @@ class LinkedList {
if (!this.head) {
return null;
}
let address = this.head;
while (address.next) {
address = address.next;
}
return address;
return this.tail;
}

getAt(index) {
Expand Down Expand Up @@ -104,8 +111,10 @@ class LinkedList {
count -= 1;
}

previous.next = new Node(element, previous.next);
return null;
const node = new Node(element, previous.next);
previous.next = node;
this.size += 1;
return node;
}

removeAt(index) {
Expand All @@ -129,21 +138,18 @@ class LinkedList {

const node = address;
previous.next = address.next.next;
this.size -= 1;
return node;
}

length() {
let address = this.head;
let count = 0;
while (address) {
count += 1;
address = address.next;
}
return count;
return this.size;
}

delete() {
this.head = null;
this.tail = this.head;
this.size = 0;
}
}

Expand Down