从单链表中删除元素(javascript)

dvtswwa3  于 2023-03-16  发布在  Java
关注(0)|答案(4)|浏览(105)

我在做一个CodeFights问题,试图从一个值为k的单链表中移除元素。
下面是我所拥有的(l是列表,k是值):

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)  
}

CodeFight测试投是3 -〉1 -〉2 -〉3 -〉4 -〉5。最终结果应该是1 -〉2 -〉4 -〉5。但是我的“---”控制台日志一直返回“空”(根据CodeFights控制台)。
我的“+++”控制台日志在每个循环中返回了正确的标题和元素。
我一直在为这件事烦恼,你知道这里缺了什么吗?

webghufk

webghufk1#

如果删除第一个节点,则需要返回列表。
然后你需要一个循环来获取下一个not,而这个值是找不到的。
最后,您需要检查最后一个节点是否存在,如果找到值,则将下一个节点分配给last next属性。

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; }
qyswt5oh

qyswt5oh2#

试试这个:

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;
}
ygya80vv

ygya80vv3#

这也可以通过递归来完成:

function removeKFromList({value, next}, k) {       
   if(value === k){
      return next ? removeKFromList(next, k) : null;
   } else {
      return {
        next : removeKFromList(next, k),
        value
       };
  }
 }
6ovsh4lw

6ovsh4lw4#

易于理解的解决方案:

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 

}

相关问题