中文描述:
给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。并在删除该节点后,返回表头。

样例
给定 1->2->3->4,和节点 3,返回 1->2->4。

English Version:
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.

Example
Given 1->2->3->4, and node 3. return 1->2->4

以下内容转自http://www.cnblogs.com/bakari/p/4013812.html
这是一道广为流传的Google面试题,考察我们对链表的操作和时间复杂度的了解,咋一看这道题还想不出什么较好的解法,但人家把题出在这,肯定是有解法的。一般单链表删除某个节点,需要知道删除节点的前一个节点,则需要O(n)的遍历时间,显然常规思路是不行的。在仔细看题目,换一种思路,既然不能在O(1)得到删除节点的前一个元素,但我们可以轻松得到后一个元素,这样,我们何不把后一个元素赋值给待删除节点,这样也就相当于是删除了当前元素。可见,该方法可行,但如果待删除节点为最后一个节点,则不能按照以上思路,没有办法,只能按照常规方法遍历,时间复杂度为O(n),是不是不符合题目要求呢?可能很多人在这就会怀疑自己的思考,从而放弃这种思路,最后可能放弃这道题,这就是这道面试题有意思的地方,虽看简单,但是考察了大家的分析判断能力,是否拥有强大的心理,充分自信。其实我们分析一下,仍然是满足题目要求的,如果删除节点为前面的n-1个节点,则时间复杂度为O(1),只有删除节点为最后一个时,时间复杂度才为O(n),所以平均的时间复杂度为:(O(1) * (n-1) + O(n))/n = O(1);仍然为O(1).

/*** Definition for ListNode.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int val) {*         this.val = val;*         this.next = null;*     }* }*/
public class Solution {/*** @param node: the node in the list should be deleted* @return: nothing*/public void deleteNode(ListNode node) {node.val = node.next.val;node.next = node.next.next;}
}

LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点相关推荐

  1. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  2. Lintcode372 Delete Node in the Middle of Singly Linked List solution 题解

    [题目描述] Implement an algorithm to delete a node in the middle of a singly linked list, given only acc ...

  3. lintcode-easy-Delete Node in the Middle of Singly Linked List

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  4. 876. Middle of the Linked List - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构 ...

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

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

  7. Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

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

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

  9. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

最新文章

  1. 简述nodejs、npm及其模块在windows下的安装与配置
  2. 30秒或更短的时间内弄懂的有用CSS代码片段
  3. PHP——获取上传文件的后缀名
  4. 给新手的 11 个 Docker 免费上手项目
  5. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 7丨字节面试真题【难度困难】
  6. MEF初体验之八:过滤目录
  7. LINUX下oracle数据导入导出方法
  8. 初学RFID-破解学校水卡
  9. 痞子衡嵌入式:MCUXpresso Config Tools初体验(Pins, Clocks, Peripherals)
  10. window10计算机策略,Win10秘笈:重置组策略/安全策略命令大全
  11. python 爬阳光高考高校数据
  12. 每天脑子里莫名其妙冒出来的话(常见于早上起床之后)持续记录,这也是日记的一种
  13. InnoDB一棵B+树能存多少行数据?为什么要用B+树?而不是其他树?
  14. ctf.show 萌xin区杂项
  15. NOI WC 2019 小结
  16. 计算机组成原理-哈工大刘宏伟(第一篇)
  17. abp zero mysql_2、ABPZero系列教程之拼多多卖家工具 更改数据库为Mysql
  18. hadoop之mapreduce教程+案例学习(一)
  19. git提交空目录的方法
  20. b站如何进行屏幕录制?录屏最值得尝试的几款软件

热门文章

  1. 【阅读笔记】联邦学习实战——联邦学习医疗健康应用案例
  2. VBA智慧办公9——图例控件教程
  3. 从零开始学习JavaScript:轻松掌握编程语言的核心技能①
  4. 移动互联网巨头扫尾战:流量黑洞效应愈演愈烈
  5. 幸福婚礼策划PPT模板
  6. mysql两万字精华总结
  7. 一个游戏设计师需要知道哪些知识?
  8. Matlab 批量fig转png(并按原文件名保存)
  9. MSComm控件的几个疑难问题汇总
  10. java小游戏龟兔赛跑,Java_多线程_简单模拟龟兔赛跑