删除链表的节点

要删除的就是这个node节点,后续节点值往前移,删除最后一个节点即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
var deleteNode = function (node) {
if (!node) return;
let cur = node;
while (cur) {
cur.val = cur.next.val;
if (!cur.next.next) {
cur.next = null;
}
cur = cur.next;
}
};

本站由 ao 使用 Stellar 1.29.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。