[Leetcode19] Remove Nth Node From End of List
视频讲解 http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html
(1)定义两个指针
ListNode fast = head;
ListNode slow = head;
(2)将快指针向前移动N步
(3.1)判断此时快指针是否已经到达尽头,如果是,头节点就是要删除的节点,返回head.next。
(3.2)将快慢两个指针同时以相同的速度往前移动,当快指针走到尽头的时候,慢指针的下一个位置就是倒数第N个节点,将慢指针next指向next.next.
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head;
ListNode slow = head; for(int i=0;i<n;i++){
fast = fast.next;
} if(fast == null){
head = head.next;
return head;
} while(fast.next != null){
fast = fast.next;
slow = slow.next;
} slow.next = slow.next.next;
return head;
}
}
[Leetcode19] Remove Nth Node From End of List的更多相关文章
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 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 ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 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 ...
- leetcode-algorithms-19 Remove Nth Node From End of List
leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...
- 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 ...
- 【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 + ...
随机推荐
- angularjs中ng-change使用方法
ng-change需与ng-model结合使用,官网说明如下:Note, this directive requires ngModel to be present. <label for=&q ...
- Python MySQLdb在Linux下的快速安装
在家里windows环境下搞了一次 见 python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...
- Java数据库——事务处理
在数据库中执行5条SQL语句,这些SQL语句本身需要保持一致,即要么同时成功,要么同时失败 事务基本操作 //============================================= ...
- 个人js
1.网页右侧地图浮动楼层,超过100px就显示 $(window).scroll(function(){ ){ //距顶部多少像素时,出现返回顶部按钮 $("#floor").fa ...
- js生成一个以零开头的八位数并且依次递增
条件:1八位数:2.以领开头:3.末尾要依次递增 <script type="text/javascript">function Account(num,a){ // ...
- Exercise16_22.java
import javax.swing.*;import java.awt.*;import java.awt.event.*; public class Exercise16_22 extends J ...
- Android中怎么用this
在JAVA程序中似乎经常见到“this”,自己也偶尔用到它,但是到底“this”该怎么用,却心中无数!很多人一提起它,就说“当前对象”,可到底什么是当前对象,是什么当前对象,他自己也不清楚.现在让大家 ...
- Logback LogBack
1.简介 LogBack是一个日志框架,它与Log4j可以说是同出一源,都出自Ceki Gülcü之手.(log4j的原型是早前由Ceki Gülcü贡献给Apache基金会的) 1.1 LogBac ...
- [Unity] 常用技巧收集
Unity 屏幕旋转 void Update () { //处理横向两个方向旋转 if(Input.deviceOrientation == DeviceOrientation.LandscapeLe ...
- wampserver 2.5安装pear win8.1
集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql 5.6.17 apache 2.4.9 ...