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 ...
随机推荐
- 2017-2018-2 《网络对抗技术》20155322 Exp6 信息搜集与漏洞扫描
[-= 博客目录 =-] 1-实践目标 1.1-实践介绍 1.2-实践内容 1.3-实践要求 2-实践过程 2.1-Google hacking & ZoomEye 2.2-DNS.IP信息收 ...
- BZOJ1068_压缩_KEY
题目传送门 区间DP,设f[i][j][0/1]为i~j区间的压缩情况,1表示在插入了一个M. code: /********************************************* ...
- 学习笔记:Oracle的trace文件可见性
隐藏参数: _trace_files_public 参数 trace文件的默认权限: - r w - r - - - - - 如果设定 trace_files_public参数为 true, 则 t ...
- python基础学习1-类,对象
class Foo:#定义类 def mail(self,email,message):#定义类的方法 print('发送邮件给%s! 信息:%s'% (email,message)) return ...
- 04-cookies 会话跟踪技术
1.会话跟踪技术 1.Http协议的无状态保存 会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应 2 .会话路径技术使用Cookie或session完成 我们知道HTTP协 ...
- 4539: [Hnoi2016]树
4539: [Hnoi2016]树 链接 分析: 主席树+倍增. 代码: #include<cstdio> #include<algorithm> #include<cs ...
- 5308: [Zjoi2018]胖
5308: [Zjoi2018]胖 链接 分析: 题目转化为一个点可以更新多少个点,一个点可以更新的点一定是一个区间,考虑二分左右端点确定这个区间. 设当前点是x,向右二分一个点y,如果x可以更新到y ...
- JAVAWEB 遍历mysql结果集时 字段为0、false、null的问题
foreach遍历查询mysql中的tinyint字段时一直查都是各种0,false,null 发现原来是实体类中的变量名和mysql中的列名不一样出的bug 所以说列名和实体类中的相关变量名是要保持 ...
- Python中re模块详细介绍
正则的介绍及应用实例详解 """ 1.什么是正则 正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串, 正则就是用来去一个大的字符串中匹配 ...
- Qt-网易云音乐界面实现-9 照片墙功能
最近车也买了,不过倒是没有想象的那么开心,车真的是想消耗品啊. 写这个专题了,本来是想好好的磨练一下自己,不过可能要在在理就GG了.腻味了. 还是先看下效果图吧 这个照片墙还差点东西,不过我个人认为需 ...