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.

最近特别喜欢解决链表方面的问题,感觉指针指来指去还是挺有意思的,而且在解决指针问题时有好多技巧来降低时间复杂度和空间复杂度。

上面这个题目就是一个比较典型的用双指针来解决问题的例子。按照正常的想法:一次遍历怎么可能做到定位这个指针呢?小白的想法是先计算指针的长度length吧,然后从前往后遍历(length-n)个节点,则下一个节点就是我们要删除的节点。这样最起码要遍历两遍。

如果我们有两个指针,一个快指针和一个慢指针,快指针比慢指针快(n-1)步,那么如果快指针.next==null时,慢指针正好指向那个我们要删除的指针。这样一次遍历就能完成这个问题。怎么样?这个方法是不是很巧妙呢?代码如下:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(n <= 0 || head==null) return head;
ListNode fast = head; //快指针
ListNode slow = head; //慢指针
ListNode pre = null; while(fast.next != null){
if(n <= 1){ //快指针比慢指针快n-1步
pre = slow;
slow = slow.next;
}
fast = fast.next;
n--;
}
if(slow == head) head = head.next;//如果删除的是头指针
else pre.next = slow.next; //删除的不是头指针
return head;
}
}

LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List的更多相关文章

  1. 【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:        ...

  2. 【Leetcode】【Easy】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 ...

  3. 【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 ...

  4. LeetCode OJ 292.Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  5. LeetCode OJ 292.Nim Gam148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种 ...

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

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

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

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

  8. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

  9. 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 + ...

随机推荐

  1. C++线程安全的单例模式

    1.在GCC4.0之后的环境下: #include <iostream> using namespace std;template <typename T>class Sing ...

  2. [转] Android root 原理

    欢迎转载,转载请注明出处:http://www.cnblogs.com/lanrenxinxin/p/5572640.html 0x00 关于root linux和类Unix系统的最初设计都是针对多用 ...

  3. 32bit程序在64bit操作系统下处理重定向细节(转自http://bbs.pediy.com/showthread.php?t=89054)

    1. 64bit操作系统的重定向机制以及目的 在64bit操作系统中,为了无缝兼容32bit程序的运行,64bit的Windows操作系统采用重定向机制.目的是为了能让32bit程序在64bit的操作 ...

  4. HDU 5863 cjj's string game

    $dp$,矩阵加速. 设$dp[i][j][0]$表示:长度为$i$的两个字符串,之前还未出现过长度为$m$相同的,目前为止最后$j$个是相同的. 设$dp[i][j][1]$表示:长度为$i$的两个 ...

  5. Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]

    关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入“mysql -uroot -pmyadmin”后出现以下错误: “Access ...

  6. 【Flash ANE iOS】关于Flash ANE在iOS上面遇到的一些问题

    1.下载Flash Builder: https://www.adobe.com/support/downloads/detail.jsp?ftpID=5516 2.如何生成p12文件: http:/ ...

  7. hdu_5965_扫雷(递推)

    题目链接:hdu_5965_扫雷 题意: 中文,还是自己看吧. 题解: 现场赛这题用的状压DP过的,不过现在想想当时还确实想复杂了,冷静下来仔细思靠一下,其实第i-1个确定了,那么第i个也是确定的,可 ...

  8. hdu_5890_Eighty seven(bitset优化DP)

    题目链接:hdu_5890_Eighty seven 题意: 50个数,10W个询问,每次问删掉第i,j,k个数后,是否存在一种选10个数和为87的方案,只需要输出 ’Yes’ 或者 ’No’ 题解: ...

  9. zookeeper(1)

    参考文档:zookeeper中文网 一.介绍安装 zookeeper 是一个高效的分布式协调服务,它暴露了一些公用服务,比如命名/配置/同步控制/群组服务等.我们可以使用ZK来实现一些功能,例如:达成 ...

  10. Android RIL Log

    转载: 要调试 RIL,最好的方法就是打开 radio的log: $ adb logcat -b radio 最好加上 log语法亮度工具coloredlogcat.py ,一些常见的LOG TAG要 ...