Skip to content

Instantly share code, notes, and snippets.

@hillal20
Last active July 1, 2020 04:32
Show Gist options
  • Save hillal20/ca57c3f09602d1f2dd5d699bf589d7cd to your computer and use it in GitHub Desktop.
Save hillal20/ca57c3f09602d1f2dd5d699bf589d7cd to your computer and use it in GitHub Desktop.
class Node {
constructor(x,next){
this.value = x;
this.next = next;
}
}
class LinkedList {
constructor(){
this.head = null;
this.tail = null;
}
addToHead(x){
// empty and not empty
const newNode = new Node(x,null);
// no node
if(this.head === null && this.tail === null ){
this.head = newNode;
this.tail = newNode;
}
//one node
else{
newNode.next = this.head;
this.head = newNode;
}
}
addToTail(x){
const newNode = new Node(x, null);
// no node
if( this.head === null && this.tail === null){
this.tail = newNode;
this.head = newNode;
return;
}
// one node or more
else {
this.tail.next = newNode;
this.tail = newNode;
}
}
removeFromHead(){
if(!this.head){
return
}
// move the head to the next node
this.head = this.head.next
}
removeFromtail(){
let current = this.head.next;
let previous = this.head;
if(!this.tail){
return
}
else if(current === null){
this.tail = null;
this.head = null;
}
else{
// we move along the chain
while(current.next !== null){
previous = current;
current = current.next;
}
// we release the last node
this.tail = previous;
this.tail.next = null;
}
}
getSize(){
let count = 0;
let current = this.head;
if(current === null ) console.log(" count ==> 0 ");
// loop through all nods from head to tail
while(current !== null){
++count;
current = current.next;
}
console.log('size ===> ', count);
return count;
}
// remove at index
removeAtIndex(index){
if(this.tail === this.head && this.tail === null){
console.log("empty");
return;
}
if(this.head === this.tail && index === 0 ){
this.head = null;
this.tail = null;
return;
}
if(index === 0){
this.head = this.head.next
return;
}
let previous = this.head;
let current = this.head.next;
const size = this.getSize();
/*
we start the count from 1 because
previous is the one is counting for us , and
it has one aready one (this.head)
*/
let count = 1;
/**
n1 - n2 - n3 -n4 -n5 ....
remeber that prevoius is at n1.
we are making the condition count < index
because the index start from 0, and the count start from 1.
So let say we want the 4th node to be pulled out, then the index is
3, and the count is 4.
the condition for the loop to stop at the count 2, means prvious will
be moving only 2 nodes ahead , so previous will be at n3, and current at n4
we make previous leapfrog n4 to connect to n5 (current.next)
*/
while(count < index){
previous = current;
count++;
current = current.next;
}
previous.next = current.next;
}
// printing
printValues(){
let current = null;
if(this.head === null ){
return null;
}
current = this.head;
while(current !== null ){
console.log(current.value);
current = current.next;
}
return;
}
// remove random value x;
removeValue(x){
// empty list
if( this.head === null) return "it is empty";
// there is only one node and it value = x
if(this.head === this.tail && this.head.value === x){
this.head = null;
this.tail = null;
return;
}
// the first node has value of x ; ===> "@"
if(this.head.value === x ){
this.head = this.head.next;
}
let previous = this.head ;
let current = this.head.next;
//keep moving looking for x in the rest of the nodes
while(current !== null){
// we want to check the current, because previous is check in ===> "@"
if(current.value === x) {
current = current.next;
previous.next = current;
continue;
}
previous = current;
current = current.next
};
// the list is finished
return;
}
}// end of LinkedList
const ll = new LinkedList();
ll.addToHead(7);
ll.addToHead(8);
ll.addToHead(9);
ll.addToTail(1)
ll.addToTail(2)
ll.addToTail(3)
ll.addToTail(4)
ll.addToHead(3)
// ll.removeFromHead()
// ll.removeFromHead()
// ll.removeFromtail()
// ll.removeValue(4);
ll.removeValue(3);
ll.removeValue(7);
ll.removeAtIndex(4);
//ll.removeAtIndex(3);
// ll.removeValue(7)
ll.getSize();
ll.printValues()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment