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 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.
解法:(参考博客:http://www.cnblogs.com/tjuloading/p/4648652.html)
将当前结点伪装成下一个结点,然后删除下一个结点即可。(真是太巧妙了,以前都没想到过这种删除方法)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
LeetCode:237的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- Java实现 LeetCode 237 删除链表中的节点
237. 删除链表中的节点 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 – head = [4,5,1,9],它可以表示为: 示例 1: ...
- Java for LeetCode 237 Delete Node in a Linked List
Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...
- (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 ...
- 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 ...
- 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 ...
- leetCode:237 删除链表的结点
删除链表的结点 编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数 ...
随机推荐
- openstack(liberty):部署实验平台(二,简单版本软件安装 part2)
继续前面的part1,将后续的compute以及network部分的安装过程记录完毕! 首先说说compute部分nova的安装. n1.准备工作.创建数据库,配置权限!(密码依旧是openstack ...
- Windows环境变量
说明:系统文件盘为C盘,操作系统为Windows XP.登录用户名为weste.计算机名为icech 说明:不同的操作系统如Windows XP和Windows 2000相对应的一些路径是不同的,这里 ...
- 03 在Linux下安装Myeclipse及Tomcat(含下载)
测试环境: 主机系统:Win 7 虚拟机:VMware workstation 11.1.0 虚拟机OS: centos 6.5 64位 Kernel 2.6.32-431-e16.x86_64 My ...
- HackerRank "Vertical Rooks"
Please note: VROOK cannot go back-ward - that leads to a simple variation to Game of Nim: just XOR. ...
- 【centos7】设置开机自启动服务--systemd
centos7使用systemd管理开机自启动服务,不提倡rc.local. 假设现在有2个服务,分别为: my111: my222: 希望在开机时自动启动my222服务,但是my222启动需要my1 ...
- 样本方差:为嘛分母是n-1
在样本方差计算式中,我们使用Xbar代替随机变量均值μ. 容易证明(参考随便一本会讲述样本方差的教材),只要Xbar不等于μ,sigma(Xi-Xbar)2必定小于sigma(Xi-μ)2. 然而,要 ...
- VS 使用Sql Server 数据库增删改查
/// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString&quo ...
- git 使用详解(8)-- tag打标签
打标签 同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做.本节我们一起来学习如何列出所有可用的标签,如何新建标签,以 ...
- kafka_2.11-0.8.2.2的搭建
一.下载官网的压缩包~ 修改conf/server.properties host.name=10.10.224.12 (修改为主机ip,不然服务器返回给客户端的是主机的hostname,客户端并不 ...
- Codeforces Round #365 (Div. 2) Chris and Road
Chris and Road 题意: 给一个n个顶点的多边形的车,有速度v,人从0走到对面的w,人速度u,问人最快到w的时间是多少,车如果挡到人,人就不能走. 题解: 这题当时以为计算几何,所以就没做 ...