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 ...
随机推荐
- JVM思考-ClassLoader.loadClasshe和Class.forName区别
JVM思考-ClassLoader.loadClasshe和Class.forName区别 目录:JVM总括:目录 见博客第四节:JVM总括四-类加载过程.双亲委派模型.对象实例化过程
- JavaSE 初学进度条JProgressBar
预备知识 创建进度条类后将其直接加入JFrame看看效果 public class JProgressBarDemo2 { public static void main(String args[]) ...
- Mac下Tomcat安装与Intellij IDEA配置Tomcat
Mac下Tomcat安装与Intellij IDEA配置Tomcat 一 安装 1 下载地址:https://tomcat.apache.org/download-90.cgi 2 将压缩包解压后移至 ...
- left join on 和where中条件的放置位置
转自:http://blog.csdn.net/muxiaoshan/article/details/7617533 select * from td left join (select case_ ...
- 软件推荐-有限元开发软件FELAC
首页:http://yuanjisuan.cn/ 有限元语言及其编译器(Finite Element Language And it’s Compiler),以下简称FELAC是中国科学院数学与系统科 ...
- python基本数据类型之字符串(四)
python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...
- IntelliJ IDEA使用心得之Maven项目篇(转)
IntelliJ IDEA使用心得之Maven项目篇 今天和大家分享下,在IDEA中打开Maven项目的方法. 对于新版的IDEA可以直接打开Maven项目,但是对于旧版的IDEA需要使用Mave ...
- EF db first 获取表名称
一直以来,使用DB FIRST的方式,想得到表名,最后一直不得其法.直到昨天晚上,反编译自己的程序集的时候,突然发现EF表结构和数据实体类的映射关系存在什么地方.然后就有了这篇文章. 咱们一步步来. ...
- Android开发 - ImageView加载Base64编码的图片
在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 ... 这些应用 ...
- Socket通信的Demo
https://blog.csdn.net/shankezh/article/details/70763579