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.


题解:快慢指针的思想,就像找到链表中点或者判断链表是否有环一样,是很经典的思想。

设置fast指针比slow指针多走n步,然后slow和fast一起前进,当fast指向null的时候,slow就指向从后往前数的第n个元素了。

但是要删除某个节点,最好的是知道它前面的节点,所以slow实际比fast慢n+1步。

代码如下:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null)
return null; ListNode slow = new ListNode(0);
ListNode answer = slow;
slow.next = head;
ListNode fast = head;
for(int i = 0;i < n;i++){
if(fast == null)
return null;
fast = fast.next;
}
while(fast != null){
slow = slow.next;
fast = fast.next;
} //delete what slow.next
slow.next = slow.next.next;
return answer.next;
}
}

【leetcode刷题笔记】Remove Nth Node From End of List的更多相关文章

  1. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  2. [刷题] 19 Remove Nth Node From End of List

    要求 给定一个链表,删除倒数第n个节点 示例 1->2->3->4->5->NULL , n=2 1->2->3->5 边界 n是从0还是从1计 n不合 ...

  3. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. 【LeetCode每天一题】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:        ...

  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 ex ...

  7. 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, ...

  8. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  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, G ...

随机推荐

  1. 自学宝典:10个学习Android开发的网站推荐

    1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...

  2. 解决ubuntu中文乱码问题

    方法一: Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在 /etc/environment中看到:sudo gedit /etc/environment可以看到如下内容:PATH= ...

  3. com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: serviceError([class java.lang.String]) 异常

    在使用spring cloud 的 Hystrix 后可能会遇到 如下截图错误: 后台代码如下: 找了好一会经过分析参数方法和原方法参数步一致造成: 修改后代码如下:

  4. 用C#来学习唐诗三百首和全唐诗

    Begin 最近把项目做完了,闲来无事,就想做点好玩的事情,刚好前几天下载了[唐诗三百首]和[全唐诗]这两个txt文件,正好用C#来整理一下. 然后导出QData格式,可以给其他软件读取. 以后弄个开 ...

  5. 大数进制转换 poj1220

    普通的做法,大数除小数. 复杂度o( log(n)*log(n) ),其实就是位数的平方. NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Lim ...

  6. 【BZOJ4269】再见Xor 高斯消元

    [BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...

  7. VS2010 fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改 ...

  8. 7.FactoryBean 和BeanFactory去区别

    FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...

  9. 监听并保存ssh账号密码

    有时候渗透搞下一台服务器的权限,想通过此服务器抓取其他服务器的ssh密码.就可以用以下方法,如果管理员通过这台服务器作为跳板登录其他服务器,密码就会记录下来. alias ssh='strace -o ...

  10. Java中byte转换int时与0xff进行与运算的原因

    http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...