LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
中文描述:
给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾)。请在在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)时间复杂度删除链表节点相关推荐
- [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 ...
- 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 ...
- 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 ...
- 876. Middle of the Linked List - LeetCode
为什么80%的码农都做不了架构师?>>> Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
最新文章
- 简述nodejs、npm及其模块在windows下的安装与配置
- 30秒或更短的时间内弄懂的有用CSS代码片段
- PHP——获取上传文件的后缀名
- 给新手的 11 个 Docker 免费上手项目
- 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 7丨字节面试真题【难度困难】
- MEF初体验之八:过滤目录
- LINUX下oracle数据导入导出方法
- 初学RFID-破解学校水卡
- 痞子衡嵌入式:MCUXpresso Config Tools初体验(Pins, Clocks, Peripherals)
- window10计算机策略,Win10秘笈:重置组策略/安全策略命令大全
- python 爬阳光高考高校数据
- 每天脑子里莫名其妙冒出来的话(常见于早上起床之后)持续记录,这也是日记的一种
- InnoDB一棵B+树能存多少行数据?为什么要用B+树?而不是其他树?
- ctf.show 萌xin区杂项
- NOI WC 2019 小结
- 计算机组成原理-哈工大刘宏伟(第一篇)
- abp zero mysql_2、ABPZero系列教程之拼多多卖家工具 更改数据库为Mysql
- hadoop之mapreduce教程+案例学习(一)
- git提交空目录的方法
- b站如何进行屏幕录制?录屏最值得尝试的几款软件