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. Linux修改SSH端口和禁止Root远程登陆

    Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...

  2. poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少

    import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...

  3. ExtJS从入门到后面肯定要抛弃

    一.ExtJs定义 ①基于JavaScript语言 ②基于JavaSwing的MVC架构 ③支持组件化.模块化设计 ④提供“本地数据源”的支持 ⑤完完善与服务端的交互机制 ⑥是最有可能拥有大规模可视化 ...

  4. HDU 5863 cjj's string game

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

  5. children

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> ...

  6. MVC中的Startup.Auth.cs、BundleConfig.cs、FilterConfig.cs和RouteConfig.cs

    一.MVC中的Startup.Auth.cs.BundleConfig.cs.FilterConfig.cs和RouteConfig.cs四个文件在app_start中 <1>Bundle ...

  7. 你应该知道的CSS文字大小单位PX、EM、PT

    这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章, 题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用 ...

  8. Android启动另一个APP时,注意disable与enable的问题

    在写游戏sdk时候遇到了一个需要在sdk中通过scheme来启动支付宝的免密支付功能,所以需要在设备中通过包名检查一下支付宝是否存在. 此时遇到了一个问题,在三星设备中可以将app给处于disable ...

  9. Java 1.0 类与对象

    1.Java中main()的作用: a.测试真正的类 b.启动Java应用程序 2. Java程序只会让对象与对象交互 3.创建对象时存储在堆中,自动回收. 4.Java无全局变量 5.Java程序由 ...

  10. 3种日志类型,微信付款反馈-->写入txt日志

    --> 接收  $GLOBALS["HTTP_RAW_POST_DATA"] ---->xml 反系列化$qr = XML_unserialize( $rowpost ...