Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Last active March 10, 2018 19:58
Show Gist options
  • Select an option

  • Save vadimkorr/c53cac9b5d9dd93076512fdcd6a750a7 to your computer and use it in GitHub Desktop.

Select an option

Save vadimkorr/c53cac9b5d9dd93076512fdcd6a750a7 to your computer and use it in GitHub Desktop.
Delete cell from linked list with respect given position
// Delete cell from linked list with respect given position
function lst() {
this.head = null;
}
lst.prototype.print = function() {
console.log('print');
let curr = this.head
while(curr) {
console.log(curr.val)
curr = curr.next
}
}
lst.prototype.push = function(val) {
console.log('push');
let curr = this.head;
if (!this.head) {
this.head = {
val: val,
next: null
}
} else {
while(curr.next) {
curr = curr.next;
}
curr.next = {
val: val,
next: null
}
}
}
lst.prototype.remove = function(ind) {
let removed = null;
if (this.head) {
let k = 0;
let curr = this.head;
if (ind === 0) {
removed = this.head;
this.head = this.head.next;
} else {
while(curr.next) {
k++;
if (k === ind) {
removed = curr;
curr.next = curr.next.next;
break;
}
curr = curr.next;
}
}
}
return removed;
}
let l = new lst();
l.push(0);
l.push(1);
l.push(2);
l.push(3);
l.push(4);
l.push(5);
l.print();
l.remove(3);
l.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment