19. Remove Nth Node From End of List (JAVA)
Given a linked list, remove the n-th node from the end of list and return its head.
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.
Follow up:
Could you do this in one pass?
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pSlow = head;
ListNode pFast = head;
for(int i = 0; i < n; i++){
pFast = pFast.next;
} if(pFast == null){ //remove first node
return head.next;
} while(pFast.next!=null){
pFast = pFast.next;
pSlow = pSlow.next;
}
pSlow.next = pSlow.next.next;
return head; }
}
解题思路:使用快慢指针,实现O(n)遍历。
19. Remove Nth Node From End of List (JAVA)的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 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, ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Java [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 ...
- 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, Give ...
随机推荐
- ICE框架双工通讯+MVVM框架测试案例
准备 开发工具 VS2015 ICE框架 https://zeroc.com/ MVVMLight框架 ICE接口文件 #include "./Identity.ice" #inc ...
- 7.2.4 else与if配对
规则是,如果没有花括号,else与离它最近的if匹配,除非最近的if被花括号括起来. 注意:要缩进"语句","语句"可以是一条简单语句或复合语句. 记住,编译器 ...
- excle删除重复项的行,自定义删除第几个
在B1输入 =COUNTIF(A$1:A1,A1) 下拉,会有数字1.2.1.2 第二步,选中B列升序排序,排序后,将B列为1的整行删除即可. 再补充下,这样是以姓名为条件来筛选,不会影响你的数据.你 ...
- ROS多根adsl叠加负载均衡PCC的做法
命令行: / ip firewall mangle1.保证访问局域网IP的时候不被PCC了.add chain=prerouting dst-address=10.1.1.0/24 action=ac ...
- ipython notebook 安装
pip install IPython pip install urllib3 pip install jupyter pip install numpy pip install matplotlib ...
- Springboot jar包外指定配置文件及原理
解决方案: 修改maven的pom.xml文件 不拷贝资源文件 <resources> <resource> <directory>src/main/resourc ...
- IO多路复用(Python)
1. select: 监听多个文件描述符(当文件描述符条件不满足时,select会阻塞),当某个文件描述符状态改变后,将该文件描述符添加到对应返回的列表 调用: fd_r_list, fd_w_lis ...
- 入坑Intel OpenVINO:记录一个示例出错的原因和解决方法
今天试用OpenVINO的例子,在过程中发现了一些其他人没有经历的坑特别记录一下. 出错时候:执行Intel OpenVINO示例的是时候,出错的提示代码: 用于 .NET Framework 的 M ...
- 对Unity一个Shader编译Bug的分析(Unrecognized sampler 'samplerunity_lightmap)
写在前面 Unity的用户量越来越大,越来越有钱,这几年摊子也铺的越来越大,所以各个版本总是有很多Bug.对于一些Bug官方在ReleaseNote里的说明是很不详细的,而对于一些渲染相关的Bug,有 ...
- Linux 下的 Docker 安装与使用
一.安装与配置 1.安装依赖包 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2.设置阿里云镜像源 sudo yum ...