如何解决删除单个链接列表上所有出现的数字
我正在尝试解决代码信号方面的一个非常标准的挑战,但是到了其中一个测试只是使站点崩溃的地步,基本上不能看到输出或结果,正如标题所指出的那样,我得到了一个单链列表,我需要仔细检查一下并删除所有出现的给定数字,这是到目前为止的代码:
// Singly-linked lists are already defined with this interface:
// class ListNode<T> {
// value: T;
// next: ListNode<T>;
//
// constructor(value: T) {
// this.value = value;
// this.next = null;
// }
// }
//
function removeKFromList(head: ListNode<number>,k: number): ListNode<number> {
if (head === null) {
return null;
}
// We move the head in the case it matches the searched element
// until there aren't more matches
while (head.value === k && head.next) {
head = head.next;
}
//Here the normal aproach of having 2 vars to check and
// jump the next pointer
let x = head;
let y = head.next;
while (x.next) {
// console.log(` x: ${x.value} y: ${y.value}`);
if (y !== null && y.value === k) {
if (y !== null && y.next !== null) {
x.next = y.next;
x = y.next;
y = x.next;
} else {
x.next = null;
}
} else {
x = x.next ? x.next : x;
y = y.next ? y.next : y;
}
}
//finally we check the head just to be sure
if (head.value === k) {
return head.next !== null ? head.next : null;
} else {
return head;
}
}
我猜gits出现内存问题,但似乎也找不到泄漏,而且已经检查了this和this,但仍然找不到我的代码的问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。