《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫《leetbook》的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看
这个是书的地址:
https://hk029.gitbooks.io/leetbook/
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.
思路
这是链表中非常常见的问题,众所周知,链表慢就慢在遍历查找,而对于单链表来说,每次必须从头开始搜索,这样使得链表在处理“倒数”这个概念的时候,特别无力。常规的做法必须要2遍遍历:1遍计算链表长度len,1遍搜索倒数的元素len-n。(当然,你可以通过加入链表长度变量或者使用双向链表解决这个问题。)
但是题目要求是一遍完成,有没有一遍的思路呢?其实是有的,那就是双指针或递归。
思路一 ——双指针
这里有个常用的做法去解决“倒数问题”:双指针。
双指针常用做法是:
- 一个指针用来作为参考,控制长度(作为循环停止条件)
- 一个指针延迟启动用来跑“倒数”。
第一个指针先运行n个数,然后打开第二个指针,这样,当第一个指针跑完时,第二个指针刚好跑过Len-N数,这样就找到了倒数第n个数。
注意:由于删除操作比较特殊,必须找到前一个节点才能删除下个节点,所以一般删除操作我们会构造一个虚拟节点作为开头,以防开头节点被删除。
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode newhead = new ListNode(-1); //防止头被删除
newhead.next = head;
ListNode point1 = newhead;
ListNode point2 = newhead;
for(;point1 != null;point1 = point1.next,n--) //point1 控制长度
{
if(n < 0)
point2 = point2.next; //point2延迟启动
}
point2.next = point2.next.next;
return newhead.next;
}
}
思路二 —— 递归
除了用双指针外,还可以考虑用递归,凡是这种涉及单链表插入删除操作的时候,都可以考虑用递归,因为插入和删除都需要涉及它的父亲操作。我们考虑最后一个元素是第一层,然后逐级返回,当返回到第N+1层(也就是父亲节点所在层数)就开始删除操作。
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode newhead = new ListNode(-1);
newhead.next = head;
remove(newhead,n);
return newhead.next;
}
private int remove(ListNode node, int n) {
if(node.next == null) return 1;
int level = remove(node.next,n)+1; //层数+1
if(level == n+1) //找到了父亲
node.next = node.next.next;
return level;
}
}
《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题的更多相关文章
- 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 ...
随机推荐
- spring mvc的例子
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Tomcat服务器(一)
一.tomcat目录中重要的文件: bin 存放启动和关闭的脚本 conf 存放配置文件 logs 日志文件 webapps 存放部署的项目 work 工作目录 Web应用开发好后,若想供外界访问, ...
- Python学习-30.Python中的元组(tuple)
元组使用()定义,元组一旦定义就无法修改. 元组的索引方式同列表,也是使用[]. 元组也可以进行切片操作,使用方式同列表一样. 可以说,一个没法修改的列表就是元组. 在没有修改操作的情况下,应尽可能使 ...
- Win10系统下编译OSG3.4
环境说明 1.Win10专业版.64位: 2.VS2012旗舰版:QT5.2.0: 3.cmake-3.9.0.64位: 资源准备 1.OSG3.4源码包 http://trac.opensceneg ...
- ASP.NET WEB API 返回JSON 出现2个双引号问题
前言 在使用ASP.NET WEB API时,我想在某个方法返回JSON格式的数据,于是首先想到的就是手动构建JSON字符串,如:"{\"result\" ...
- CUDA开发 - CUDA 版本
"CUDA runtime is insufficient with CUDA driver"CUDA 9.2: 396.xx CUDA 9.1: 387.xx CUDA 9.0: ...
- [JS] Ajax请求会话过期处理
对于页面来说,处理session过期比较简单,一般只需在过滤器里面判断session用户是否存在,不存在则跳转页面到登陆页即可. 对于Ajax请求来说,这个办法则无效,只能获取到登录页的html代码. ...
- JS里关于特殊字符的转义
重定向的url里含有百分号“%”,遇到了apache 找不到该文件的报错.通过查询相关文档,知道了原来是url里含有特殊字符要转码才能定位到正确的地址.比如"%"要转码为" ...
- C#远程桌面连接工具
1.注册控件:regsvr32 C:\Windows\System32\mstscax.dll. 2.添加RDP Control控件到工具箱中.我选择的RDP Client Control (redi ...
- 前端入门html(表单)
day48 配置Django项目:https://blog.csdn.net/zV3e189oS5c0tSknrBCL/article/details/79606994 <!DOCTYPE ht ...