Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enhancements on Linked lists #79

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

Merged
TheSTL merged 2 commits into master from linked-lists
Oct 13, 2019
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
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /