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. C# 判断字符串为空有哪几种方法

    Length法:);Empty法:bool isEmpty = (str == String.Empty);General法:bool isEmpty = (str == ""); ...

  2. Sublime Text 3技巧:支持GB2312和GBK编码

    From: http://blog.csdn.net/ubuntulover/article/details/21101979 Sublime Text 3与Sublime Text 2的不同 其实有 ...

  3. android代码规范和studio配置CodeStyle

    studio配置CodeStyle可以很好的帮助我们检测代码规范性,保持大家的代码统一,来看看怎么配置和使用吧 代码规范,自己公司的一套 代码规范 一.      简介 A.    目的 本文提供一整 ...

  4. uva 548 Tree(通过后序,先序重建树+dfs)

    难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ...

  5. UART Receive FIFO and Receive Timeout

    为什么UART要有FIFO? 现代的CPU运转速度越来越快.UART的波特率通常达不到10M,在没有FIFO的情况下.每次填充数据给UART 或者 从UART取数据都会占用CPU的时间.这是极大的浪费 ...

  6. SharePoint 2013 页面中window/document.onload/ready 事件不能触发的解决方案

    问题1:在SharePoint 2013页面中使用Javascript 事件window/document.onload/ready时,你会发现处理onload/ready事件的代码根本不能执行. 问 ...

  7. Visual Studio快捷键大全

    快捷键的使用可以简化大家的操作,在一定程度上提高工作的效率,下文中将为大家介绍一些VS中经常用到的快捷键,希望对大家有用. 方法/步骤   关于解决方案和项目   用于快速跳转   用于代码的文本编辑 ...

  8. geoserver PostGIS的安装和使用

    PostGIS是一个非常流行并且开源的具有空间分析能力的关系型数据库,它作为PostgreSQL数据库的一个插件.PostgreSQL是一个功能非常强大并且开源的关系型数据库.目前项目使用的版本为Po ...

  9. UnitOfWork 更新实体出错解决办法

    用UnitOfWork进行实体更新的时候,再查询实体一次,再去更新的时候会报如下错误: Attaching an entity of type 'TinyFrame.Data.DomainModel. ...

  10. Dubbo -- 系统学习 笔记 -- 示例 -- 多版本

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多版本 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间 ...