[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.
问题:只给定单向列表中的一个节点,从列表中删除该节点。
删除列表的某个元素,只知道根据头节点,和待删节点值,遍历搜索到相同值,将其删除。对于给定节点从列表中删除,没有思路。在网上看了讲解,原来这个是列表的基本操作,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 解题思路的更多相关文章
- 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 ...
- 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 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- (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. 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 ...
- 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 ...
随机推荐
- cocos2d-x 在mac下执行 demo
搞了好久,最终成功了. 记录一下 前辈的文章. 依照这个弄的 .http://blog.csdn.net/taowenyin/article/details/11750127 前辈的这个文章是在win ...
- 跟着Android学设计模式:代理(proxy)
代理模式 1.意图:为其它对象提供一种代理以控制对这个对象的訪问. 2.适用性: 远程代理:虚代理.保护代理:智能引用: Android源代码中用到了大量的代理设计模式.比方Context的设计,Bi ...
- 【c#】Form调用百度地图api攻略及常见问题
首先,在Form中调用百度地图api,我们需要使用webbrowser控件,这个在前面的文章中已经讲过了,可以参照(http://blog.csdn.net/buptgshengod/article/ ...
- LA 6450 Social Advertising
[题目] 给一个无向图,每当对某个点操作,该点以及与该点相连的点都获得标记,问标记所有点至少需要操作多少次 输入 第一行为T,表示测试数据组数 每组测试数据第一行为n(1<=n<=20)表 ...
- 应用框架 ViewPager Tab Fragment SlidingMenu
介绍 常见的应用框架 框架一:多个tab+Fragment,点击不同的tab加载不同的Fragment,不能滑动切换只能点击切换: 框架二:多个tab+ViewPager+FragmentPagerA ...
- MyEclipse修改servlet模版
找到myeclipse安装目录中的 然后把这个jar包复制到桌面 以压缩包的方式打开 之后保存, 然后把修改的这个jar包放到刚开的路径,替换已经存在的! 完成!
- OD: ASLR
ASLR,Address Space Layout Randomization,通过加载程序的时候不再使用固定的基址,从而干扰 shellcode 定位的一种保护机制,包括映像随机化.堆栈随机化.PE ...
- php获取某个目录下面文件的内容
if(!defined('PATH'))define('PATH', dirname(dirname(__FILE__)).'/');ini_set ( 'include_path', '.:' . ...
- OLEDB 连接EXCEL的连接字符串IMEX的问题(Oledb)
今天碰到一个问题需要想EXCEL表中写数据,折腾了好久才发现是IMEX惹得祸,所以记录下提醒自己,也希望大家不要出同样的错. 碰到问题:使用语句 "insert into [Sheet1$] ...
- oracle 语句
1.查询TRENDCHART_DLT表中的30条数据,统计字段FRONT01='0',BACK12='0'的条数 select sum(case when FRONT01='0' then 1 els ...