Reputation: 544
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?
Upvotes: 3
Views: 14860
Reputation: 47
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
}
Upvotes: 0
Reputation: 138457
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
};
}
}
Upvotes: 0
Reputation: 1336
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;
}
Upvotes: 0
Reputation: 386728
You 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; }
Upvotes: 2