LeetCode题解:(19) Remove Nth Node From End of List
题目说明
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
题目分析
看上去还挺简单的,由于题目要求只遍历一遍链表,我的做法是采用两个指针,保证两个指针的距离为n,这样当前面的指针到达尾部的时候,另一个指针的位置就是要移除的结点。
以下为个人实现(C++,糟糕的一坨代码):
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *new_head = new ListNode(-1);
new_head->next = head;
ListNode *p1 = new_head, *p2 = new_head;
for (int i = 0; i <= n; i++, p2 = p2->next); // p2 = p1 + n + 1
for (; p2 != NULL; p2 = p2->next, p1 = p1->next);
p2 = p1->next;
p1->next = p2->next;
delete(p2);
p2 = new_head->next;
delete(new_head);
return p2;
}
};
这里比较僵硬的一点是链表头的元素需要额外进行判断,我这种算法可以说很辣鸡了……
于是在讨论版看到了另外一种和我思路一样,但是细节不同的代码实现:
class Solution
{
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
ListNode** t1 = &head, *t2 = head;
for(int i = 1; i < n; ++i)
{
t2 = t2->next;
}
while(t2->next != NULL)
{
t1 = &((*t1)->next);
t2 = t2->next;
}
*t1 = (*t1)->next;
return head;
}
};
这份代码和我所实现的区别在于使用了二级指针,开始觉得很莫名其妙,后来看了Linus:利用二级指针删除单向链表的解释后,感觉这种做法挺巧妙的。
在第一种方法中(不使用二级指针),我所做的操作是将entry->next赋值给prev->next,然而当entry为链表头的时候就没有prev->next可以赋值了。但是实际上,任何一个链表都会有一个和prev->next同等地位的指针,那就是head指针,对于链表头元素,执行的操作可以是将head->next赋值给head。此时第二种方法(使用二级指针)的好处是它不关心entry->next究竟赋值给了谁,只关心赋值的地址,因此也免了判断entry是不是head的步骤了。
(微软笔试崩了……郁闷)
LeetCode题解:(19) Remove Nth Node From End of List的更多相关文章
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- LeetCode题解(19)--Remove Nth Node From End of List
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- LeetCode OJ 19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- leetcode个人题解——#19 Remove Nth Node From End of List
思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...
- [Leetcode][Python]19: Remove Nth Node From End of List
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...
- LeetCode:19. Remove Nth Node From End of List(Medium)
1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...
- 【LeetCode】19. Remove Nth Node From End of List
题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...
随机推荐
- Eclipse获取资源路径
一.问题: 这几天做一个单机版的数据抓取项目,之前都加载了spring或者是maven 使用[this.getClass().getClassLoader().getResource("ma ...
- 北京Uber优步司机奖励政策(4月19日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 18-[JavaScript]-函数,Object对象,定时器,正则表达式
1.函数创建 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- [NOI2018]归程 kruskal重构树
[NOI2018]归程 LG传送门 kruskal重构树模板题. 另一篇文章里有关于kruskal重构树更详细的介绍和更板子的题目. 题意懒得说了,这题的关键在于快速找出从查询的点出发能到达的点(即经 ...
- matlab GUI工作原理
例如,用GUIDE创建名为ceshi的GUI程序,其m文件的主函数有如下形式.那么,打开该GUI时,它到底是怎么运行的呢?以下略作小结,欢迎大家补充 function varargout = cesh ...
- 2_C语言中的数据类型 (七)类型限定
1.1 类型限定 1.1.1 const const是代表一个不能改变值的常量 1.1.2 volatile 代表变量是一个可能被CPU指令之外的地方改 ...
- html元素可以同时实现两个class的功能
html元素标签可以放置两个不同的class文件并分别实现效果
- Object C学习笔记1-基本数据类型说明
Objective-C数据类型可以分为:基本数据类型.对象类型和id类型.基本数据类型有:int.float.double和char类型.对象类型就是类或协议所声明的指针类型,例如:NSAutorel ...
- SQL Server Management Studio 评估期已过
SQL2008破解: (1)将SQL安装光盘(或者ISO)放进去运行,进入安装界面. (2)选择“维护”中的“版本升级”,如图: (3)按照版本升级的向导,先输入产品密钥,也就是正式企业版的序列号: ...
- 从《乱世王者》看腾讯SLG手游如何搭建完整安全服务
WeTest 导读 <乱世王者>是由腾讯旗下天美工作室群自主研发的一款战争策略手游,在经历了2015年-2017年的SLG品类手游的爆发之势下,于2017年11月21日正式公测. < ...