【一天一道LeetCode】#237. Delete Node in a Linked List
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 -> 4after calling your function.
(二)解题
题目大意:给定一个单链表中的一个节点,删除它。
解题思路一
删除给定的节点,由于不知道该节点的前驱节点,所以,可以把后面的节点值往前移,然后删除尾部最后一个节点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* pre = node;
ListNode* p = node->next;
while(p!=NULL)
{
pre->val = p->val;
if(p->next!=NULL) {//p没有到最后一个节点
pre = p;
p = p->next;
}
else break;//p->next==NULL,代表遍历到最后一个节点了
}
pre->next = NULL;
delete p;//删除p
}
};
解题思路二
既然可以改变节点值,那么可以把给定node的后继节点值赋给node,然后删除node的后继节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* temp = node->next;
node->val = temp->val;
node->next = temp->next;
delete temp;//删除node的后继节点
}
};
很奇怪,思路一的AC时间是12ms,思路二的AC时间是16ms。
想了很久都想不通,大家要是能够解释这个的,可以在下面留言!共同学习!
【一天一道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 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 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- (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 ...
- [LeetCode] 237. Delete Node in a Linked List_Easy tag: 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 ...
随机推荐
- 华科机考:a+b
时间限制:1秒空间限制:32768K 题目描述 计算a+b的和 每行包行两个整数a和b 对于每行输入对应输出一行a和b的和 输入 1 5 输出 6 吐槽:这尼玛是机考题? 代码: #include & ...
- 设置思科设备console密码、enable密码、vty登录密码
思科设备各级密码:1) console密码 SW2(config)#line console 0SW2(config-line)#password ciscoSW2(config-line)#log ...
- jQuery简单笔记
jQuery 是一个 JavaScript 库,简化了 JavaScript 的编程. 语法:$(selector).action() selector 是字符串,表示HTML元素. 对象 符号 例子 ...
- Mysql--存储引擎(MyISam & InnoDB)
Mysql 系列文章主页 =============== 查看 Mysql 支持的存储引擎: show engines; 查看当前数据库使用的存储引擎: show variables like '%s ...
- Error:Cannot build Artifact :war exploded because it is included into a circular depency
找到项目的目录 查找artifacts文件夹 删掉不是你项目名称的那个 问题出现的原因是你该项目名字了 造成tomcat发布两个网页 发布两个网页不是什么大问题 但是这两玩意地址一样 争夺资源啊冲突之 ...
- jdk1.7和jdk1.8区别
转自:http://www.2cto.com/kf/201307/225968.html 本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容. 官方文档:ht ...
- numpy.random中的shuffle和permutation以及mini-batch调整数据集(X, Y)
0. numpy.random中的shuffle和permutation numpy.random.shuffle(x) and numpy.random.permutation(x),这两个有什么不 ...
- 微信小程序基础之开源项目库汇总
awesome-github-wechat-weapp 是由OpenDigg整理并维护的微信小程序开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. (链接:ht ...
- A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes
A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes (文 ...
- 让你的代码减少三倍!使用kotlin开发Android(五) 监听器
本文同步自 博主的私人博客wing的地方酒馆 在前面的博客中,有一个栗子,是点击按钮转跳的监听器. button.setOnClickListener { val user = User(" ...