leetcode — remove-nth-node-from-end-of-list
/**
* 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的更多相关文章
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
随机推荐
- windows下Anaconda的安装与配置正解
一.下载anaconda 第一步当然是下载anaconda了,官方网站的下载需要用迅雷才能快点,或者直接到清华大学镜像站下载. 清华大学提供了镜像,从这个镜像下载速度很快,地址: https://mi ...
- [杂谈]杂谈章2 eclipse没有(添加)“Dynamic Web Project”
原因:你安装的是专门开发java项目的,而Dynamic Web Project 属于J2EE技术 第一种方法: 你要专门下载一个集成了J2EE插件的Eclipse,到eclipse官网下载相对应版 ...
- 【MarkMark学习笔记学习笔记】javascript/js 学习笔记
1.0, 概述.JavaScript是ECMAScript的实现之一 2.0,在HTML中使用JavaScript. 2.1 3.0,基本概念 3.1,ECMAScript中的一切(变量,函数名,操作 ...
- linux 基础 文件系统 用户权限
描述Linux系统的启动过程? 1.开机自检 BIOS 2.MBR引导 3.GRUB菜单 4.加载内核 5.运行init进程 6.从/etc/inittab读取运行级别 7.根据/etc/rc.sys ...
- usb协议栈学习笔记
1.usb 集线器为什么一般都是只有4个扩展口? PC的根集线器可为每个A型连接器提供5V.500mA电源.一个总线供电的外部集线器可为每个端口提供100mA电流.由于USB为为外部集线器电路分配10 ...
- JavaScript笔记基础版
1.JavaScript数组: 创建数组 var cars = new Array(); 数组的初始化: 1.1 cars[0]="Saab"; cars[1]="Vo ...
- PMP:5.项目范围管理
项目范围管理包括确保项目做且只做所需的全部工作,以成功完成项目的各个过程. 管理项目范围主要在于定义和控制哪些工作应该包括在项目内,哪些不应该包括在项目内. 核心概念: 规划范围管理:为记录如何定 ...
- ReactNative学习笔记(五)踩坑总结
已经发现的bug或者问题 Android不支持shadow属性: Animated.Image的borderRadius不生效: setNativeProps无法修改图片的source: 没有直接设置 ...
- cad.net cad启动慢? cad2008启动慢? cad启动延迟? cad卡住? cad98%卡? 默认打印机!!
默认打印机是不是联网打印机,如果cad找不到这个打印机将会很慢才打开cad的界面(它真的不是卡死了,而是找不到...) 奇妙的是桌子至今都没有利用新建线程的方式来控制这个打印机等待,而是直接在主程序上 ...
- Akka-Cluster(1)- Cluster Singleton 单例节点
关于cluster-singleton我在前面的博文已经介绍过,在这篇我想回顾一下它的作用和使用方法.首先,cluster-singleton就是集群某个节点上的一个actor.任何时间在集群内保证只 ...