【一天一道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 ...
随机推荐
- Amazon新一代云端关系数据库Aurora(上)
本文由 网易云发布. 在2017年5月芝加哥举办的世界顶级数据库会议SIGMOD/PODS上,作为全球最大的公有云服务提供商,Amazon首次系统的总结 了新一代云端关系数据库Aurora的设计实现 ...
- Python中的变量类型
原文传送门:点击
- Delphi7数据库编程之TDataSet(转)
TDataSet类由TBDEDataSet(BDE组件).TCustomADODataSet(ADO组件).TIBCustomDataSet(InterBase组件).TCustomSQLDataSe ...
- 使用JdbcTemplate过程中使用到多个参数和like模糊
项目中经常会用到模糊查询,最近使用JdbcTemplate过程中就遇到了. 一开始尝试了拼接的方式去 String sql = "select count(1) from web_users ...
- form submit提交
form内控件参数自动添加到url后,而自定义的url参数则不能添加到url后 $('#fm').form('submit', { url: 'Data/Diary.ashx?dt=' + new D ...
- ajax中基本参数应用
$(function () { $("#verificationCodeBtn").click(function () { $("#verificationCodeIma ...
- Spring cloud 学习资料整理
推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 周立 Spring Cloud 方志朋 Spring Cloud 专栏 许进 跟我学Spring Cloud 推荐网站 Spring Cl ...
- 16. 3Sum Closest(中等)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 解释session
我理解的session就是,多个页面都要使用某一个或一些数据,这时就可以用session,将数据暂时保存起来,这样其他的页面开启session,就能将那些数据拿出来使用.
- 通过ajax和spring 后台传输json数据
在通过ajax从页面向后台传数据的时候,总是返回415(Unsupported media type)错误,后台无法获取数据.如下图所示: 在尝试解决这个问题的时候,我们首先要理解一下概念: @req ...