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 ...
随机推荐
- oracle rowid 研究
SQL> create table tab01(id integer,val varchar(4)); Table created. SQL> insert into tab01 valu ...
- 用 Python 带你看《我不是药神》
我们都是小人物,我们都得了同一种病,我们都穷.——<我不是药神> 我不是程序员 我就是想求求你们,别动不动就拿篇10W+的文章来吓唬人好吗?说点有用的东西好吗?我们需要精神粮食不需要腐蚀精 ...
- Flutter - Error: 'xxx' is imported from both package...
Compiler message: lib/main.dart:77:32: Error: 'Toast' is imported from both 'package:easy_alert/src/ ...
- 从轻测到上线,WeTest与《一起来捉妖》测试方案大公开
从2016年Pokémon GO引发的AR游戏热潮开始,国内就一直在期待新的一款具备代表性的AR游戏的头部作品. 4月11日的腾讯首款AR探索手游<一起来捉妖>不仅为国内市场注入了新的活力 ...
- 常见面试算法题JS实现-设计一个有getMin功能的栈
前言: 已经确定工作了-下周一正式入职,按理说应该是可以好好浪荡一周的,但是内心总是不安,总觉得自己这个水平真的太菜了,还是趁着现在有自己的时间,赶紧多看看书,多学习学习吧orz所以把之前校招买的书, ...
- 将禅道部署到腾讯云linux 上
部署环境 :linux(腾讯云),用到了 xshell FileZilla 使用禅道集成环境lampp直接部署 1.首先下载 lampp j集成环境包.https://sourceforge.ne ...
- ci框架学习整理
-- -- 表的结构 `yi_article` -- CREATE TABLE IF NOT EXISTS `yi_article` ( `id` int(11) unsigned NOT NULL ...
- 【Unity Shader】(六) ------ 复杂的光照(上)
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Sha ...
- 前端基础之CSS(总结)
css学什么?? 主要学习选择器和属性,选择器是去找到标签的位置,属性是给标签增加需要的样式. CSS选择器 1.基本选择器: 1.标签选择器 2.ID选择器 3.类选择器(class="c ...
- sklearn 中的 Pipeline 机制
转载自:https://blog.csdn.net/lanchunhui/article/details/50521648 from sklearn.pipeline import Pipeline ...