203.移除链表元素

移除链表元素

给你一个链表的头节点 head 和一个整数 val,请你删除链表中所有满足 Node.val == val 的节点,并返回新的头节点。

解析

1
2
3
4
5
6
7
8
9
var removeElements = function (head, val) {
const dummy = new ListNode(0, head);
let curr = dummy;
while (curr.next) {
if (curr.next.val === val) curr.next = curr.next.next;
else curr = curr.next;
}
return dummy.next;
};

203.移除链表元素
https://leetcode.lz5z.com/203.remove-linked-list-elements/
作者
tickli
发布于
2024年6月14日
许可协议