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. iOS: 动态更改 backBarButtonItem 的 title

    先来看一下 UIBarItem 的 title 的描述 The title displayed on the item. You should set this property before add ...

  2. 正则grep

    什么是正则: 正则就是一串有规律的字符串. 正则三剑客: grep/egrep (egrep是grep的扩展),sed, awk grep grep过滤指定关键词 [root@localhost gr ...

  3. ASP.NET程序也能像WinForm程序一样运行[转载]

    阅读目录 开始 操作方式 支持的ASP.NET程序类别 它也是个HTTP服务器 支持远程机器访问 不受限于Windows防火墙 尊重每个人的操作习惯 内置多标签浏览器支持 启动参数及配置文件 支持 . ...

  4. 使用 UICollectionView 实现网格化视图效果

    讲解 UICollectionView 的相关链接:http://blog.csdn.net/eqera/article/details/8134986 关键操作: 效果如下: KMCollectio ...

  5. 最新Java校招面试题及答案

    本文作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向.在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几个部分: Java 基础知识点 Java 常见集合 ...

  6. 研究jenkins集成unittest成图

    jenkins搭建完毕,unittest代码编写完毕,触发unittest执行测试的脚本和任务编写完毕,接下来研究生成的结果在页面的可视化. 方案: highcharts 参考资料: http://b ...

  7. @PropertySource加载文件的两种用法以及配置文件加载顺序

    第一种: 现在我把资源文件的路径放在application.properties里 config.path=/home/myservice/config.properties @PropertySou ...

  8. 【scala】 scala 条件控制 和异常处理(二)

    1.scala 变量定义 ,var val 区别. var 定义可变变量 val 定义不可变变量,scala 推荐使用.相当于Java的final 变量. scala中包含的基本数据类型详情如下表所示 ...

  9. [web] spring boot 整合MyBatis

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  10. linux系统调用函数---12

    Linux应用编程学习笔记                                 周学伟 一.系统调用文件编程   1.文件打开函数 /*************************** ...