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. tp5的学习

    1.安装,官网下载 2.访问配置:http://localhost/App/public/ 3.入口文件,项目目录/public // 定义应用目录 define('APP_PATH', __DIR_ ...

  2. Python爬虫学习——获取网页

    通过GET请求获取返回的网页,其中加入了User-agent信息,不然会抛出"HTTP Error 403: Forbidden"异常, 因为有些网站为了防止这种没有User-ag ...

  3. mysql 创建和删除用户

    1.远程登录mysql mysql -h ip -u root -p 密码 2.创建用户 格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码 ...

  4. AsyncTask异步类的简单操作

    package com.example.day9; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; im ...

  5. level1 - unit 1 - 句子结构

    preface 学习英语做为一种爱好,就是希望有一天能够和老外流畅沟通,目前来看,日常沟通还是没有问题的和我的外教. 知识日积月累,现在就把以前学习的(从初中到现在)知识总结下.每天一更,更到单元总结 ...

  6. Win7 共享打印机 “错误:共享无法保存设置”

    原因:windows的后台防火墙服务进程[Windows Firewall{服务描述}.MpsSvc{服务名字}] 关闭了.services.smc 关闭 解决:重启服务 关闭防火墙的功能,通过控制面 ...

  7. python unittest 2

    参考资料:http://pyunit.sourceforge.net/pyunit_cn.html :http://docs.python.org/2/library/unittest.html py ...

  8. php解析mpp文件中的前置任务

    获取层级的project任务  参考 启动javabridge java -jar JavaBridge.jar SERVLET_LOCAL:8089 1.读取mpp文件 $file_path = & ...

  9. 调试Java源代码时变量的值无法追踪怎么办?

    问题:调试Java源代码时,只能看到源代码,却无法查看源代码中的变量的即时值 原因:jre为了节省空间,在打包时去掉了class文件中的调试信息. 思路:使用jdk里的src.zip源码重新编译生成完 ...

  10. 泛泰A870K去掉相机快门声音的方法

    首先ROOT手机,挂载读写,/system/media/audio/ui里面哈,把camera-click.ogg改成camera-click.ogg.bak就可以了 转载自:http://bbs.9 ...