题目: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的更多相关文章

  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. Java实现 LeetCode 237 删除链表中的节点

    237. 删除链表中的节点 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 – head = [4,5,1,9],它可以表示为: 示例 1: ...

  5. Java for LeetCode 237 Delete Node in a Linked List

    Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...

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

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

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

  9. leetCode:237 删除链表的结点

    删除链表的结点 编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数 ...

随机推荐

  1. Bouncycastle中的RSA技术以及解决之道

    一个使用bouncycastle进行安全操作的实用类 2007-04-13 12:54 import java.io.*;import java.security.*;import java.secu ...

  2. 【oracle】数据库、表空间、用户、数据表之间的关系

    来自为知笔记(Wiz) 附件列表 新建_032515_030437_PM.jpg

  3. 【freemaker】之自定义变量,特殊变量 globals ,循环对象取值

    entity public class Employee { private Integer id; private String name; private Integer age; private ...

  4. 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. strong标签与b标签的区别

    关于html标签中b和strong两个的区别. 用在网页上,默认情况下它们起的均是加粗字体的作用,二者所不同的是,<b>标签是一个实体标签,它所包围的字符将被设为bold(粗体),而< ...

  6. 原生视觉差滚动---js+css;

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 黄聪:PHP 防护XSS,SQL,代码执行,文件包含等多种高危漏洞

    版本:v1.1更新时间:2013-05-25更新内容:优化性能功能说明: 可以有效防护XSS,sql注射,代码执行,文件包含等多种高危漏洞. 使用方法: 将waf.php传到要包含的文件的目录 在页面 ...

  8. (C/C++ )Interview in English - Virtual

    Q: What is virtual function?A: A virtual function or virtual method is a function or method whose be ...

  9. 为什么引用不了App_Code里的类

    在Web应用程序中不能通过右键项目-〉”添加“-〉”添加ASP.NET文件夹“方式添加 .因为Web应用程序中App_Code就不存在 .不过可以通过手动的方式创建,添加一个文件夹命名为App_Cod ...

  10. 创建指定日期java Date对象

    import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import ...