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.

问题:只给定单向列表中的一个节点,从列表中删除该节点。

删除列表的某个元素,只知道根据头节点,和待删节点值,遍历搜索到相同值,将其删除。对于给定节点从列表中删除,没有思路。在网上看了讲解,原来这个是列表的基本操作,O(1) 时间即可完成。看了我对列表还不够熟悉。

     void deleteNode(ListNode* node) {

         node->val = node->next->val;
node->next = node->next->next; }

题目提到给定的节点,不会是最末尾节点。这样看了这个算法是有缺陷的,因为不能删除最后一个元素。其实不是,只需要在原本的列表最后,插入一个符号代表 NULL ,那么这个算法就对整个列表的完整操作。

参考资料:

[LeetCode]Delete Node in a Linked List, 书影

[LeetCode] 237. Delete Node in a Linked List 解题思路的更多相关文章

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

  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. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

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

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

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

随机推荐

  1. xml--通过DOM解析XML

    此文章通过3个例子表示DOM方式解析XML的用法. 通过DOM解析XML必须要写的3行代码. step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) step 2:获得具体的dom解 ...

  2. OpenStack Neutron DVR L2 Agent的初步解析 (一)

    声明: 本博客欢迎转载,但请保留原作者信息! 作者:林凯 团队:华为杭州OpenStack团队 OpenStack Juno版本号已正式公布,这是这个开源云平台的10个版本号,在Juno版的Neutr ...

  3. HTML编辑器UEditor的简单使用

    參考自:http://ueditor.baidu.com/website/document.html 关于HTML编辑器,试过FCKeditor,升级版的CKeditor,还有TinyMCE,近期在尝 ...

  4. linux vim 个性化设置(.vimrc)

    set sw=4   set ts=4   set et   set smarttab   set smartindent   set lbr   set fo+=mB   set sm   set ...

  5. HashSet与HashMap

    HashSet底层由HashMap实现 Hash表:存放链表表头的数组 HashSet的值存放于HashMap的key上,而HashMap的value统一为PRESENT(private static ...

  6. 第一章:在IDEA里搭建基于Forge的Minecraft mod开发环境

    <基于1.8 Forge的Minecraft mod制作经验分享> 网上关于Forge开发环境搭建的文章其实有不少,但大都是基于Eclipse的. 作为用Java开发的环境,怎么能没有ID ...

  7. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  8. 封装函数--->切换,添加,删除class

    var obj={}; obj.className='a b c d active'; //切换class function toggle(obj,className) { var str=obj.c ...

  9. 你真的了解 console 吗

    对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...

  10. GDI+基础(3)

    常用图形绘制 <%@ Page ContentType="image/gif" Language="C#" %> <!--ContentTyp ...