algorighm/leetcode/中等/删除链表中的节点.js

39 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
有一个单链表的 head我们想删除它其中的一个节点 node。
给你一个需要删除的节点 node 。你将 无法访问 第一个节点 head。
链表的所有值都是 唯一的,并且保证给定的节点 node 不是链表中的最后一个节点。
删除给定的节点。注意,删除节点并不是指从内存中删除它。这里的意思是:
给定节点的值不应该存在于链表中。
链表中的节点数应该减少 1。
node 前面的所有值顺序相同。
node 后面的所有值顺序相同。
自定义测试:
对于输入,你应该提供整个链表 head 和要给出的节点 node。node 不应该是链表的最后一个节点,而应该是链表中的一个实际节点。
我们将构建链表,并将节点传递给你的函数。
输出将是调用你函数后的整个链表。
*/
import { LinkListNode as ListNode, LinkList } from '../../list/index.js';
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
const deleteNode = function (node) {
node.val = node.next.val;
node.next = node.next.next;
};
const node3 = new ListNode(3);
const node2 = new ListNode(2, node3);
const head = new ListNode(1, node2);
deleteNode(node2);
console.log(new LinkList(head).toString());