LeetCode:237 删除链表中的节点

在这里插入图片描述核心:删除链表中的节点数字。

/**
 * 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) {
    node.val=node.next.val;
    node.next=node.next.next;
};

核心在于:比如4->5->6->7
要删除6的话,是吧,题目也说不能删除末尾的是吧,那就只能先把第四个节点的值赋值给上一个节点的值(覆盖),然后是在删除最后一个节点就行了啊. node.next=node.next.next;因为没有第五个节点所以第五个null覆盖掉第四个节点的值就搞定了。

猜你喜欢

转载自blog.csdn.net/qq_37805832/article/details/107420845