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

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

法一:思路是:从node 节点开始,把后面节点的值赋给前面的节点,直到到表尾;

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
ListNode pos = node.next;
if (pos==null)
node=null;
else {
while (pos!=null){
node.val = pos.val;
pos = pos.next;
if (pos==null)
node.next = null;
else
node = node.next;
}
}
}
}

法二:将要删除节点后一个节点的值赋给要删除的节点,然后删除后一个节点。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
if(node.next==null){
node=null;
return;
}
node.val = node.next.val;
node.next = node.next.next;
}
}

[Leetcode]237. Delete Node in a Linked List -David_Lin的更多相关文章

  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. 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. (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 ...

  5. 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 ...

  6. 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 ...

  7. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

    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. Linux 体系结构

    Linux 体系结构 Linux 嵌入式系统的组成 层次结构图   bios 1.硬件检测 2.初始化系统设备 3.装入os 4.调os向硬件发出的指令 bsp 板级支持包 硬件相关 开发板原理图 开 ...

  2. Spring Cloud微服务笔记(二)Spring Cloud 简介

    Spring Cloud 简介 Spring Cloud的设计理念是Integrate Everything,即充分利用现有的开源组件, 在它们之上设计一套统一的规范/接口使它们能够接入Spring ...

  3. [POJ2259]Team Queue (队列,模拟)

    2559是栈,2259是队列,真的是巧啊 题意 模拟队列 思路 水题 代码 因为太水,不想打,发博客只是为了与2559照应,于是附上lyd的std #include <queue> #in ...

  4. 关于A2C算法

    https://github.com/sweetice/Deep-reinforcement-learning-with-pytorch/blob/master/Char4%20A2C/A2C.py ...

  5. VMware vsphere client报错问题

    今天出现一种情况,搞了很久,重装client都不行,原来很简单,防止再犯.     异常信息:"VirtualInfrastructure.Utils.ClientsXml"的类型 ...

  6. sortable的基本属性

    所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象   ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象    ...

  7. jquery项目中一些常用方法

    1.获取url中的参数 function getUrlParam(name) {    var reg = new RegExp("(^|&)" + name + &quo ...

  8. 编译部署 Mysql 5.7

    1.环境准备 RHEL7.4(最小化安装)  64bit   2G 内存 (1G 内存编译将近一个小时) 磁盘空间 15G 以上. 配置为本地yum 源 从MySQL5.7版本开始,安装MySQL需要 ...

  9. 在Centos中部署nginx

    准备工作: nginx的安装依赖openSSL,zlib和pcre Openssl下载地址: http://www.openssl.org/ zlib下载地址: http://www.zlib.net ...

  10. git如何避免push/pull时输入密码

    今天在搭建git服务器的时候,一切顺利,但是就是在git push的时候老是要输入密码,太烦了,然后百度搜索了一下,总结了主要有如下三种方法: 方法1 git config --global cred ...