I'm doing a CodeFights problem, trying to remove elements from a singly linked list that has the value k.
Below is what I have (l is the list and k is the value):
function removeKFromList(l, k) {
//figure out the head of the new list so we can reference it later
var head = l;
while (head.value === k){
head = head.next;
}
var node = head;
var temp = null;
while (node && node !== null) {
if (node.next.value === k){
temp = node.next.next;
node.next.next = null;
node.next = temp;
}
node = node.next;
console.log("+++", head)
}
console.log("---", head)
}
The CodeFight test cast is 3 -> 1 -> 2 -> 3 -> 4 -> 5. The final result would have been 1 -> 2 -> 4 -> 5. But my '---' console log keeps returning "Empty" (according to the CodeFights console).
My '+++' console log returns the correct head with element each loop.
I've been pulling my hair out over this, any idea what is missing here?
I'm doing a CodeFights problem, trying to remove elements from a singly linked list that has the value k.
Below is what I have (l is the list and k is the value):
function removeKFromList(l, k) {
//figure out the head of the new list so we can reference it later
var head = l;
while (head.value === k){
head = head.next;
}
var node = head;
var temp = null;
while (node && node !== null) {
if (node.next.value === k){
temp = node.next.next;
node.next.next = null;
node.next = temp;
}
node = node.next;
console.log("+++", head)
}
console.log("---", head)
}
The CodeFight test cast is 3 -> 1 -> 2 -> 3 -> 4 -> 5. The final result would have been 1 -> 2 -> 4 -> 5. But my '---' console log keeps returning "Empty" (according to the CodeFights console).
My '+++' console log returns the correct head with element each loop.
I've been pulling my hair out over this, any idea what is missing here?
Share Improve this question edited Dec 9, 2017 at 19:11 Kaisin Li asked Dec 9, 2017 at 18:55 Kaisin LiKaisin Li 5443 gold badges8 silver badges26 bronze badges 4- please add the example of the list es well, as the call of the function with values. – Nina Scholz Commented Dec 9, 2017 at 18:57
- I included to test case. The list is already given as a SLL so I wouldn't have to create one. Hope this helps – Kaisin Li Commented Dec 9, 2017 at 19:12
- 1 one problem is the first node, you need to assign the next node as start of the list. this works only of the list is supplied via an object. – Nina Scholz Commented Dec 9, 2017 at 19:16
-
1
Your while loop condition should be
while (node && node.next != null)
. – David G Commented Dec 9, 2017 at 19:17
4 Answers
Reset to default 2You need to return the list if you delete the first node.
Then you need a loop for taking the next not while the value is not found.
At last you need a check if the last node exist and if the value is found, then assign the next node to the last next property.
function removeNode(list, value) {
var node = list,
last;
if (node && node.value === value) {
return node.next;
}
while (node && node.value !== value) {
last = node,
node = node.next;
}
if (last && node.value === value) {
last.next = node.next;
}
return list;
}
var list = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5, next: { value: 6, next: { value: 7, next: null } } } } } } };
list = removeNode(list, 5);
console.log(list)
list = removeNode(list, 1);
console.log(list)
list = removeNode(list, 7);
console.log(list)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try this:
function myRemove(l, k){
if(l == null){
return l;
}
while(l.value == k){
l = l.next;
}
thisNode = l;
nextNode = thisNode.next;
while(nextNode != null){
if(nextNode.value == k){
thisNode.next = nextNode.next;
// No more nodes, ie last node was to be removed
if(thisNode.next == null)
break;
}
thisNode = thisNode.next;
nextNode = thisNode.next;
}
return l;
}
This could be also done by recursion:
function removeKFromList({value, next}, k) {
if(value === k){
return next ? removeKFromList(next, k) : null;
} else {
return {
next : removeKFromList(next, k),
value
};
}
}
Easy to understand solution :
remove(index) {
if (index < 0 || index > this.length) return undefined
if (index === 0) return this.shift()
if (index === this.length - 1) return this.pop()
let previousNode = this.get(index - 1)
let removed = previousNode.next
previousNode.next = removed.next
previousNode.next = currentNode.next
this.length--
return removed
}
get(index) {
if (index < 0 || index >= this.length) return null
let current = this.head
let count = 0
while (count !== index) {
current = current.next
count++
}
return current
}
pop() {
if (!this.head) return undefined
let current = this.head
let newTail = current
while (current.next) {
newTail = current
current = current.next
}
this.tail = newTail
this.tail.next = null
this.length--
if (this.length === 0) {
this.head = null
this.tail = null
}
return current
}
shift() {
if (!this.head) return undefined
let currentHead = this.head
this.head = current.next
this.length--
if (this.length === 0) {
this.tail = null
}
return currentHead
}