Problem: 移除距离尾节点为n的节点. 
 
使用快慢指针,(由于是对链表进行操作,并且是距离尾节点距离为n,因此将快指针移动n个,慢指针移动到fast.next==null即可)
 
参考代码
package leetcode_50;

/***
*
* @author pengfei_zheng
* 移除距离尾节点为n的节点
*/
public class Solution19 { public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head.next == null) {
return null;
}
int counter = 1;
ListNode start = new ListNode(0);
start.next = head;//保存头指针
ListNode fast = start;
ListNode slow = start;
while (fast.next != null) {//循环体
fast = fast.next;//快指针移动
if (counter <= n) {
counter++;//计数器
} else {
slow = slow.next;//慢指针移动
}
}
slow.next = slow.next.next;//移除
return start.next;
}
}

LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)的更多相关文章

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

  2. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  3. [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, ...

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

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

  6. (链表 双指针) leetcode 19. Remove Nth Node From End of List

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  7. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  8. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

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

随机推荐

  1. 安装tensorflow出现问题的解法

    在ubuntu14.04用pip安装tensorflow-gpu 安装1.3.0遇到问题 1.安装tensorflow出现Cannot uninstall 'six'.问题的解法 https://bl ...

  2. mac osx voice over的使用

    mac下的filezilla是一个很奇葩的应用,奇葩在哪?有一次我不知道怎么操作的,把filezilla搬到到了窗体顶部,结果被mac的菜单栏挡住了标题栏,然后再也无法移动窗体了,以下是我为了移动它做 ...

  3. TensorFlow安装,升级,基本操作

    一. 安装 ubuntu 16 python 2.7 pip install tensorflow 测试安装完成效果: 查看tensorFlow版本python import tensorflow a ...

  4. Jenkins入门知识

    1  修改jenkins的根目录,默认地在C:\Documents and Settings\AAA\.jenkins . .jenkins ├─jobs│  └─JavaHelloWorld│    ...

  5. java的final关键字——修饰变量

    final修饰的变量不可变,指的是引用不可变,(除基本类型)而不是内容. final修饰的成员变量必须被初始化

  6. Cannot call sendError() after the response has been committed - baiyangliu

    当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...

  7. go和python互调

    https://www.cnblogs.com/huangguifeng/p/8931837.html   Python调用go编写的高性能模块 https://yq.aliyun.com/artic ...

  8. UVA 1232 - SKYLINE(线段树)

    UVA 1232 - SKYLINE option=com_onlinejudge&Itemid=8&page=show_problem&category=502&pr ...

  9. SpringMVC -- 梗概--源码--贰--拦截器:Interceptor

    附:实体类 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...

  10. iOS 播放gif动态图的方式探讨

    原文链接:http://my.oschina.net/u/2340880/blog/608560 摘要iOS中没有现成的接口来展示gif动态图,但可以通过其他的方式来处理gif图的展示.iOS中播放g ...