【一天一道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 ...
随机推荐
- 新版Eclipse打开jsp、js等为文本编辑,没有JSP Editor插件问题
刚从官网下载安装的Eclipse Java Oxygen.2但是打开的jsp文件尽然默认文本编辑器打开,就js文件也是一样,纳闷! 网上搜索一番,原来缺少web开发相关工具, 下面给插件安装方法: 1 ...
- CentOS7快速配置nginx node mysql8.0
目录: (一)基础准备 (二)安装node (三)安装nginx (四)安装mySql8.0 (五)整体配置 (六)安装PM2守护进程 (一)基础准备1.1 概述 服务器操作系统为 centos7.4 ...
- C语言第二次作业 ,
一:修改错题 1输出带框文字:在屏幕上输出以下3行信息. 将源代码输入编译器 运行程序发现错误 错误信息1: 错误原因:将stido.h拼写错误 改正方法:将stido.h改为stdio.h 错误信息 ...
- google-gson 解析json
http://www.cnblogs.com/jianyungsun/p/6647203.html 在JSON官网我们可以查看到各个语法对json的支持,对于java来说比较成熟的是google-gs ...
- js变量的一点认识
js中变量包含两种不同数据类型的值,基本类型值(简单的数据段)和引用类型值(可能由多个值组成的对象).那么他们在保存方式和复制变量值是上有什么不同呢? 一.保存 只能给引用类型的值动态添加属性,不能给 ...
- ES6(es2015)新增实用方法汇总
Array 1.map() [1,2,3,4].map(function(item, index, array){ return item * 2; }) 对数组中的每一项执行一次回调函数,三个参数 ...
- java里String类为何被设计为final
前些天面试遇到一个非常难的关于String的问题,"String为何被设计为不可变的"?类似的问题也有"String为何被设计为final?"个人认为还是前面一 ...
- Dynamics 365 你所期待的子网格编辑终于来了
Dynamics 365的online版本已经在11月1号发布了,on-premises版也在没几天后发布,今天略看了一眼 what's new 一眼就看到了 editable grids,这个不用我 ...
- Docker Volume 之权限管理(一)
摘要: Volume数据卷是Docker的一个重要概念.数据卷是可供一个或多个容器使用的特殊目录,可以为容器应用存储提供有价值的特性.然而Docker数据卷的权限管理经常是非常令人困惑的.本文将结合实 ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...