/**
* Source : https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/
*
* Created by lverpeng on 2017/7/11.
*
* Given a linked list, remove the nth node from the end of list and return its head.
*
* For example,
*
* Given linked list: 1->2->3->4->5, and n = 2.
*
* After removing the second node from the end, the linked list becomes 1->2->3->5.
*
* Note:
* Given n will always be valid.
* Try to do this in one pass.
*
*/
public class RemoveNthNodeFromEndOfList { /**
* 移除倒数第n个node,但是链表是单向的,只能从前向后,要找到倒数第n个需要技巧
* 设置两个指针,faster、slower,初始化都指向head,移动faster n次,然后同时移动slower,
* faster指向tail的时候,slower就指向了倒数第n个
*
* 假设链表共有t个元素,faster第一次移动n个之后,剩下的就是t - n,这个时候slower从开始移动,就是移动t - n次,也就是倒数第n个
*
* 考虑特殊情况:
* 链表为空
* 链表总长度小于n,也就是faster提前遇到null
*
* n不能等于链表的长度,因为无法判断t是多少,也就无法判断faster = null的时候是 n == t还是 n > t
*
* @param head
* @param n
* @return
*/
public Node removeNode (Node head, int n) {
if (head == null || n <= 0) {
return null;
}
Node faster = head;
Node slower = head;
for (int i = 0; i <= n; i++) {
if (faster == null) {
return null;
}
faster = faster.next;
}
if (faster == null) {
// n == 链表的size
head = head.next;
return head;
}
while (faster != null) {
faster = faster.next;
slower = slower.next;
}
slower.next = slower.next.next;
return head; } private static class Node {
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "null" : next.value) +
'}';
}
} public static void main(String[] args) {
RemoveNthNodeFromEndOfList removeNthNodeFromEndOfList = new RemoveNthNodeFromEndOfList();
Node head = new Node();
Node last = head;
last.value = 1; for (int i = 2; i <= 5; i++) {
Node node = new Node();
node.value = i;
last.next = node;
last = node;
} Node newHead = removeNthNodeFromEndOfList.removeNode(head, 6); Node pointer = newHead;
while (pointer != null) {
System.out.println(pointer);
pointer = pointer.next;
}
}
}

leetcode — remove-nth-node-from-end-of-list的更多相关文章

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

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

  2. [LeetCode] 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 ...

  3. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

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

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

  7. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  8. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

随机推荐

  1. playframework 一步一步来 之 日志(一)

    日志模块是一个系统中必不可少的一部分,它可以帮助我们写程序的时候查看错误信息,利于调试和维护,在业务面,它也可以记录系统的一些关键性的操作,便于系统信息的监控和追踪. play的日志是基于logbac ...

  2. 【APP测试(Android)】--升级更新

  3. 阿里巴巴Java开发规约插件安装使用指南

    编码规范插件安装使用指南 阿里技术公众号于今年的2月9日首次公布<阿里巴巴Java开发规约>,瞬间引起全民代码规范的热潮,后又发布了PDF的终极版,大家踊跃留言,期待配套的静态扫描工具开放 ...

  4. addEventListener()方法

    ★JS事件的捕获阶段和冒泡阶段: 讨论的主要是两个事件模型:IE事件模型与DOM事件模型 IE内核浏览器的事件模型是冒泡型事件(没有捕获事件过程),事件句柄的触发顺序是从ChildNode到Paren ...

  5. LOJ-10105(欧拉回路模板,套圈法,递归)

    题目链接:传送门 思路: (1)用邻接表存储有向图和无向图,有向图和无向图的每条边均站两个单元,无向图有正向边和反向边的区分. (2)有向图有欧拉回路:所有点的入度=出度: 无向图有欧拉回路:所有点的 ...

  6. sublime将python的运行结果在命令行显示

    sublime将python的运行结果在命令行显示 为什么这么折腾? 因为每次查看输出结果都要上下拖动窗口,很烦. 将build system修改为 { "cmd": [" ...

  7. Exception、Error、运行时异常与一般异常有何异同

    转自博客  https://blog.csdn.net/m0_37531231/article/details/79502778 一.开场白 对于程序运行过程中的可能出现异常情况,java语言使用一种 ...

  8. win7安装vs2017时闪退

    最近用公司的笔记本电脑,装win10发现太卡,无奈最终选择安装win7系统,本以为系统安装成功了,接下来只要安装下开发环境:vs2017 sqlserver等就好,结果在安装vs2017的时候,一直出 ...

  9. 定时任务 Wpf.Quartz.Demo.5 (升级版)

    老规矩:先把全部源码上传,见本文底部. 相对于Demo3的区别,就是能自动加载继承了IJob的任务,任务主体程序分离. 在exe执行文件的同级下建一个MyJobs的文件夹,每次会自动扫描该文件夹下的J ...

  10. 【接口时序】7、VGA接口原理与Verilog实现

    一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1. FPGA型号:Xilinx公 ...