237.Delete Node in a Linked List


描述

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
It is guaranteed that the node to be deleted is not a tail node in the list.

删除链表中的当前节点。

题解

没有头节点,也就无法获取上一个节点,那么思路只能将当前节点作为头节点:

  • 复制下一个节点给当前节点
  • 将下一个节点作为需要删除的节点
var deleteNode = function(node) {
    node.val = node.next.val;
    node.next = node.next.next;
};

文章作者: 阿汪同学
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 阿汪同学 !
评论
  目录