Remove Nth Node From End of List leetcode java
题目:
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.
题解:
这道题也是经典题,利用的是faster和slower双指针来解决。
首先先让faster从起始点往后跑n步。
然后再让slower和faster一起跑,直到faster==null时候,slower所指向的node就是需要删除的节点。
注意,一般链表删除节点时候,需要维护一个prev指针,指向需要删除节点的上一个节点。
为了方便起见,当让slower和faster同时一起跑时,就不让 faster跑到null了,让他停在上一步,faster.next==null时候,这样slower就正好指向要删除节点的上一个节点,充当了prev指针。这样一来,就很容易做删除操作了。
slower.next = slower.next.next(类似于prev.next = prev.next.next)。
同时,这里还要注意对删除头结点的单独处理,要删除头结点时,没办法帮他维护prev节点,所以当发现要删除的是头结点时,直接让head = head.next并returnhead就够了。
代码如下:
1 public static ListNode removeNthFromEnd(ListNode head, int n) {
2 if(head == null || head.next == null)
3 return null;
4
5 ListNode faster = head;
6 ListNode slower = head;
7
8 for(int i = 0; i<n; i++)
9 faster = faster.next;
if(faster == null){
head = head.next;
return head;
}
while(faster.next != null){
slower = slower.next;
faster = faster.next;
}
slower.next = slower.next.next;
return head;
}
Remove Nth Node From End of List leetcode java的更多相关文章
- Remove Nth Node From End of List [LeetCode]
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 《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 (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解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
随机推荐
- 【知了堂学习笔记】java 编写几种常见排序算法
排序的分类: 一.交换排序 所谓交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动. 1.冒泡 ...
- 【运维实战】一次linux日志分割之路——将日志按照每小时进行分割,并按照“日期-小时”格式保存
是这样的,现在需要对nginx的access.log进行按照每小时进行分割,并且最好还要能够以 “日期+时间”的形式,命名保存. 两点,一个是按照每小时进行分割,一个是将日志以“日期+时间”的形式进行 ...
- 【基础知识】.Net基础加强 第四天
一. 显示实现接口 1. 显示实现接口的目的:为了解决法方法重名的问题. 2. 显示实现接口必须是私有的,不能用public 3. (复习)类中成员不写访问修饰符默认是private:类如果不写访问修 ...
- JavaScript基础 :学习javascript的原因
JavaScript是世界上最流行的脚本语言,因为你在电脑.手机.平板上浏览的所有的网页,以及无数基于HTML5的手机App,交互逻辑都是由JavaScript驱动的. 简单地说,JavaScript ...
- virtualenv虚拟环境安装不同版本的django
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...
- 数据储存为base64编码,如何实现模糊搜索
假设字段title存储的是经过base64编码后的字符串,$key是存搜索关键字的变量 则普通的查询方法, select * from 表名 where title like '$key'; 无法正确 ...
- CF912D Fishes 期望 + 贪心
有趣的水题 由期望的线性性质,全局期望 = 每个格子的期望之和 由于权值一样,我们优先选概率大的点就好了 用一些数据结构来维护就好了 复杂度$O(k \log n)$ #include <set ...
- HNOI2018 两天扫雷训练营
Day -1 不知道干什么 学了下插头DP,随意看了几道题 Day 0 还是不知道干什么 打了一天的模板,1700多行.... 随意学了下回文树 Day 1 上午:各种丢人(好像没人注意) 电脑一开就 ...
- codevs 1191 树轴染色 线段树区间定值,求和
codevs 1191 树轴染色 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.codevs.cn/problem/1191/ Des ...
- HDU 5154 Harry and Magical Computer bfs
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...