题目链接

题目大意:删除单链表中倒数第n个节点。例子如下:

法一:双指针,fast指针先走n步,然后slow指针与fast一起走,记录slow前一个节点,当fast走到链表结尾,slow所指向的指针就是所找的节点。代码如下(耗时8ms):

     public ListNode removeNthFromEnd(ListNode head, int n) {
int cnt = 0;
ListNode fast = head, slow = head, preSlow = head;
while(cnt < n) {
fast = fast.next;
cnt++;
}
while(fast != null) {
fast = fast.next;
preSlow = slow;
slow = slow.next;
}
//删除的是第一个节点
if(preSlow == slow) {
head = preSlow.next;
}
//删除的不是第一个节点
preSlow.next = slow.next;
return head;
}

19.Remove Nth Node From End of List---双指针的更多相关文章

  1. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

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

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

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

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

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

  5. [LeetCode] 19. 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 ...

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

  7. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  8. Java [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 ...

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

  10. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

随机推荐

  1. [三]SpringBoot 之 热部署

    如下配置 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  2. 题解 P1808 【单词分类_NOI导刊2011提高(01)】

    大家用的方法都太好了!! 蒟蒻小金羊来发一篇玄学堆排. STL大法好! (附有核心code详解,完整code) 核心:两次排序,第一次自我排序,第二次整体排序. 核心code1: string str ...

  3. javascript的解析顺序

    一.javascript的解析顺序 我们大家所理解的代码的执行顺序都是从上到下的,但是实际上确不是这样的.我们看一下下面的代码. 1 alert(a);2 var a = 1;如果执行顺序是从上到下的 ...

  4. Redis存储系统

    一.QuickStart 1.Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string ...

  5. redis2.4.conf配置文件中文释意

    # Redis示例配置文件 # 注意单位问题:当需要设置内存大小的时候,可以使用类似1k.5GB.4M这样的常见格式: # # 1k => 1000 bytes # 1kb => 1024 ...

  6. C 位段,位域

    百度百科解释的很好,地址如下: http://baike.baidu.com/link?url=9yb5izRj6S1TzsN--EVexN7BAEzSn3jGctSgcoLTwVfaCXmtfMj3 ...

  7. 框架----Django之ModelForm组件

    ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...

  8. 【ST】【CF855B】 Marvolo Gaunt's Ring

    传送门 Description 给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\), 找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \ ...

  9. 【DP】【CF31E】 TV Game

    传送门 Description 给你一个长度为\(2n\)的数字,每次可以从左侧选一个数字,加入连接到一个数字\(A\)或另一个数字\(B\)后面.\(A,B\)初始为\(0\).\(A\)与\(B\ ...

  10. SSH 阿里云服务器

    1.在服务机上操作 创建要远程登录的用户和密码 sudo adduser username    正在添加用户“username”... 正在添加新组“username”(1001)... 正在添加新 ...