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 ...
随机推荐
- 选择困难症的福音——团队Scrum冲刺阶段-Day 7
选择困难症的福音--团队Scrum冲刺阶段-Day 7 今日进展 测试代码 将界面设计完后放app使用示意图于此 今日贡献量 严域俊 吴恒佚 曾程 刘辰 邓煜坤 3.5 3.5 3.3 3.6 3 贡 ...
- 基于Zxing的二维码的二维码扫描之横屏扫描
最近项目条码扫描要改为横屏,网上所搜了一下,然后发现我写的需要改动几行代码就可以了,还是很给力的. 如未查看之前的代码,请移步: 基于Zxing的二维码生成和二维码扫描 修改下面写代码就可以实现横屏条 ...
- SpringBoot编写自定义Starter
根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...
- loadtxt()函数的糟心历程
原计划:导入一个csv文件,然后算出平均值 import numpy as np c=np.loadtxt('d:\python36\data.csv', delimiter=',', usecols ...
- kbmmw中向服务器端传递对象的一种简单方式
运行环境:delphi 10.2+kbmmw 5.6.20 在kbmmw 的老版本中,要向服务器传送一个本地的对象,一般都需要进行一些转换,例如通过序列化的方式. 在新版的kbmmw中这一切都变的很简 ...
- ubuntu 应用添加进环境变量
BG:公司同事使用的电脑系统大多为windows ,有部分mac和Ubuntu(我就是那个部分Ubuntu),某些情况为了统一格式,便下载了一些解压即可使用的软件,但是每次点开文件夹然后点开程序很繁琐 ...
- 解决跨域脚本攻击 XSS
配置Content Security Policy 传送门:作者: 阮一峰 http://www.ruanyifeng.com/blog/2016/09/csp.html [前端安全]JavaS ...
- Linux 第六天
1)locate 在文件资料库中查找文件(需要文件资料库中有,新建的文件查不到,需要手动更新,updatedb.查不到/tmp目录下的文件) 语法:locate 文件名 常用选项: -i:无视大小写查 ...
- 《mysql必知必会》学习_第18章_20180807_欢
第18章 全文本搜索 P121 #创建一个新表,对表的列进行定义,定义之后,MySQL自动维护该索引# create table productnotes ( note_id int NOT ...
- 三.mysql表的完整性约束
mysql表的完整性约束 什么是约束 not null 不能为空的 unique 唯一 = 不能重复 primary key 主键 = 不能为空 且 不能重复 foreign key ...