视频讲解  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的更多相关文章

  1. Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

  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 ...

  3. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  4. 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 ...

  5. 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 ...

  6. 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. ...

  7. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  8. 【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 ...

  9. 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 + ...

随机推荐

  1. sudo和rpm命令

    sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等.这样不仅减少了root用户的登录 和管理时间,同样也提高了安全 ...

  2. SQL Server编程(01)流程控制

    批处理 应用程序向SqlServer发送的一组命令,Sql Server会将其编译成一个可执行单元,称为执行计划,执行计划中的语句每次执行一条. 每个不同的批处理用GO命令分割.GO命令不是SQL语句 ...

  3. 深入揭秘HTTPS安全问题&连接建立全过程

    作者:[已重置]链接:https://zhuanlan.zhihu.com/p/22142170来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作为开发者必备的网络安全 ...

  4. ObjectInputStream类和ObjectInputStream类的使用

    版权声明:本文为博主原创文章,未经博主允许不得转载. ObjectInputStream和ObjectInputStream类创建的对象被称为对象输入流和对象输出流. 创建文件输出流代码: FileO ...

  5. 安装Flask

    安装Flask步骤 输入网址https://bitbucket.org/pypa/setuptools](https://bitbucket.org/pypa/setuptools,回车后进入setu ...

  6. ecshop后台,listtable.js使用

    1.先载入listtable.js 2.html代码 a.  <a href="javascript:listTable.sort('goods_number'); "> ...

  7. python模块time&datetime&json & picle&14.logging等

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  8. JS网页顶部进度条demo

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. Serializer序列化/反序列化DateTime少了8小时问题解决

    1.举例子 JavascriptSerializer serializer = new JavascriptSerializer(); DateTime now = DateTime.Parse(&q ...

  10. Mysql存储过程查询结果赋值到变量的方法

    Mysql存储过程查询结果赋值到变量的方法   把查询结果赋值到变量,大部分情况下使用游标来完成,但是如果明确知道查询结果只有一行(例如统计记录的数量,某个字段求和等),其实可以使用set或into的 ...