Last active
March 10, 2018 19:58
-
-
Save vadimkorr/c53cac9b5d9dd93076512fdcd6a750a7 to your computer and use it in GitHub Desktop.
Delete cell from linked list with respect given position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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