Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Given linked list -- head = [4,5,1,9], which looks like following:

Example 1:

Input: head = [,,,], node =
Output: [,,]
Explanation: You are given the second node with value , the linked list should become -> -> after calling your function.

Example 2:

Input: head = [,,,], node =
Output: [,,]
Explanation: You are given the third node with value , the linked list should become -> -> after calling your function.

Note:

 The linked list will have at least two elements.
All of the nodes' values will be unique.
The given node will not be the tail and it will always be a valid node of the linked list.
Do not return anything from your function.

解题思路:与之前删除链表结点不同的是:这道题只给了我们要删除的那个结点,并没有给出链表的头结点,所以无法找到上一个结点,顺链删除此结点。我们可以将下一个结点的值覆盖在当前节点上,删除下一个节点即可。

C++:

  void deleteNode(ListNode* node) {
ListNode* p=node->next;
node->val=p->val;
node->next=p->next;
delete p;
}

Java:

 public void deleteNode(ListNode node) {
ListNode p=node.next;
node.val=p.val;
node.next=p.next;
}

LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java的更多相关文章

  1. [LeetCode] 237. Delete Node in a Linked List 解题思路

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  2. 17.leetcode 237. Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  3. LeetCode 237. Delete Node in a Linked List (在链表中删除一个点)

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. [LeetCode] 237. Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. (easy)LeetCode 237.Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  6. leetcode 237 Delete Node in a Linked List python

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  7. [Leetcode]237. Delete Node in a Linked List -David_Lin

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  8. [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  9. LeetCode 237 Delete Node in a Linked List 解题报告

    题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...

随机推荐

  1. php中include和require的区别(整理)

      require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 requi ...

  2. 设计模式—模板方法(template method)

    一.定义 百度百科给的定义:定义一个操作中的算法骨架(稳定),而将一些步骤延迟到子类中(变化).Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 如何做到 ...

  3. GAN试验记录.

    1.GAN目标函数不收敛,参数难调 2.数据集与生成集比例 3.生成四不像,模式崩塌

  4. LibreOffice字体问题解决;从window复制到Ubuntu

    拷贝或下载windows系统的Fonts字体集到对应的linux系统下;以ubuntu16.04系统为例. 1.进入windows系统,到C:WindowsFonts目录,复制拷贝自己需要的字体(也可 ...

  5. java--遇到NoSuchMethodError通用解决思路

    https://www.cnblogs.com/xiaoMzjm/p/4566672.html 最近接手新项目,项目一跑,NoSuchMethodError蹦出来了,好不容易解决了,换一个电脑,NoS ...

  6. C# EnumHelper Enum的值,Description,ToString()的相互转换

    首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate {         [Descriptio ...

  7. linux下目录简介——/proc

    1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...

  8. Kuberneteser二进制安装与配置(二)

    环境:Centos7 版本:Kubernetes v1.11.4   一.下载Kubernetes   1)下载 wget https://github.com/kubernetes/kubernet ...

  9. 18.13 Uboot分析与移植

    18.13.1 使用JLink烧写Nor Flash JLink只支持烧写NOR Flash,不支持烧写Nand Flash. 1.准备工作:JLink的USB口接到电脑上,JLink的JTAG口用排 ...

  10. Handlebars.js registerHelper

    Handlebars.registerHelper('link', function (text, url) { text = Handlebars.Utils.escapeExpression(te ...