Given a linked list, remove the nth node from the end of list and return its head.

For example,

  1. Given linked list: 1->2->3->4->5, and n = 2.
  2.  
  3. 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时,慢指针正好指向那个我们要删除的指针。这样一次遍历就能完成这个问题。怎么样?这个方法是不是很巧妙呢?代码如下:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode removeNthFromEnd(ListNode head, int n) {
  11. if(n <= 0 || head==null) return head;
  12. ListNode fast = head; //快指针
  13. ListNode slow = head; //慢指针
  14. ListNode pre = null;
  15.  
  16. while(fast.next != null){
  17. if(n <= 1){ //快指针比慢指针快n-1步
  18. pre = slow;
  19. slow = slow.next;
  20. }
  21. fast = fast.next;
  22. n--;
  23. }
  24. if(slow == head) head = head.next;//如果删除的是头指针
  25. else pre.next = slow.next; //删除的不是头指针
  26. return head;
  27. }
  28. }

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. C3P0连接池参数详解

    <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> < ...

  2. 字符串ID转换成字符串名字

    select  U.CnName+','  from f_splitstr('1828,1055333,1,1035681,752,494,22549,219,23860,478,23453,677, ...

  3. ZZNU 1988: Sn

    题目描述 给你两个数 n, p(0 < n,p <= 10^15); a1 = 1;  a2 = 1+2;  a3 = 1+2+3;  ... an = 1+2+3+...+n    Sn ...

  4. MySQL 5.5 禁用 innodb

    MySQL 5.5 禁用 innodb 编辑: my.ini 添加: default-storage-engine=MYISAM skip-innodb

  5. HDU 5857 Median

    因为原序列是排列好了的,那么只要看一下给出的两个区间相交的情况,然后分类讨论一下,O(1)输出. #pragma comment(linker, "/STACK:1024000000,102 ...

  6. 《JS权威指南学习总结--8.5 作为命名空间的函数》

    内容要点:    函数作用域的概念:在函数中声明的变量在整个函数体内都是可见的(包括在嵌套的函数中),在函数的外部是不可见的.不在任何函数内声明的变量是全局变量,在整个JS程序中都是可见的. 在JS中 ...

  7. linshi_temp_erweima_html

    <!doctype html><html><head><meta charset="utf-8"><meta content= ...

  8. openwrt 更改 debug 等级(hostapd)

    https://wiki.openwrt.org/doc/devel/debugging 调试hostapd,其中hostapd的调试等级如下: # Levels (minimum value for ...

  9. jquery-ui-multiselect 的使用

    @model Gd.NetSign.Controllers.DTO.WsaleFundManageDTO @{ ViewBag.Title = "ShowDUYUN"; //Lay ...

  10. java的String类型为什么是final

    (转自:http://www.cnblogs.com/ikuman/archive/2013/08/27/3284410.html) 最佳答案: 主要是为了“效率” 和 “安全性” 的缘故.若 Str ...