Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active January 13, 2024 05:23
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 49 You must be signed in to fork a gist
  • Save bradtraversy/c38f029e5f9e56a19c393d3a3b1e1544 to your computer and use it in GitHub Desktop.
Save bradtraversy/c38f029e5f9e56a19c393d3a3b1e1544 to your computer and use it in GitHub Desktop.
JS Linked List Class
// Construct Single Node
class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
// Create/Get/Remove Nodes From Linked List
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// Insert first node
insertFirst(data) {
this.head = new Node(data, this.head);
this.size++;
}
// Insert last node
insertLast(data) {
let node = new Node(data);
let current;
// If empty, make head
if (!this.head) {
this.head = node;
} else {
current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
// Insert at index
insertAt(data, index) {
// If index is out of range
if (index > 0 && index > this.size) {
return;
}
// If first index
if (index === 0) {
this.insertFirst(data);
return;
}
const node = new Node(data);
let current, previous;
// Set current to first
current = this.head;
let count = 0;
while (count < index) {
previous = current; // Node before index
count++;
current = current.next; // Node after index
}
node.next = current;
previous.next = node;
this.size++;
}
// Get at index
getAt(index) {
let current = this.head;
let count = 0;
while (current) {
if (count == index) {
console.log(current.data);
}
count++;
current = current.next;
}
return null;
}
// Remove at index
removeAt(index) {
if (index > 0 && index > this.size) {
return;
}
let current = this.head;
let previous;
let count = 0;
// Remove first
if (index === 0) {
this.head = current.next;
} else {
while (count < index) {
count++;
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.size--;
}
// Clear list
clearList() {
this.head = null;
this.size = 0;
}
// Print list data
printListData() {
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
const ll = new LinkedList();
ll.insertFirst(100);
ll.insertFirst(200);
ll.insertFirst(300);
ll.insertLast(400);
ll.insertAt(500, 3);
// ll.clearList();
// ll.getAt(2);
ll.printListData();
@DeveloperShad
Copy link

DeveloperShad commented Nov 18, 2021

at line 46 I think if condition should be if (index< 0 | | index > this.size) return ;

@dilmurodov
Copy link

at line 46 I think if condition should be if (index< 0 | | index > this.size) return ;

I think it should be if (index < 0 && index > this.size) return;

@racbacn
Copy link

racbacn commented Mar 14, 2022

at line 46 I think if condition should be if (index< 0 | | index > this.size) return ;

I think it should be if (index < 0 && index > this.size) return;

No, it should be "||". He was correct.

@Thomas-Aragaw
Copy link

Thomas-Aragaw commented Aug 9, 2022

Great explanation @bradtraversy , I agree with @DeveloperShad that it should "||"

@KaiIemsawat
Copy link

at line 46 I think if condition should be if (index< 0 | | index > this.size) return ;

Agreed,
Also, I think that should apply to 'removeIndex()' and 'getAt()' as well

@tenzinwoz
Copy link

In the line no 69,70 we are just making changes/adding to the local variable current and previous but i was wondering how these changes are added to this.head . If any one can explain me this ?

@chihab1991
Copy link

In the line no 69,70 we are just making changes/adding to the local variable current and previous but i was wondering how these changes are added to this.head . If any one can explain me this ?

I think becausein line 60 we setting current to LinkedList.head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment