题目:移除linked-list从尾到头的第N个元素

自我思路:因为题目给出的N是从链表尾开始计算的,单链表不存在从子指向父亲的反向指针,因此我先计算链表的整个长度len,然后用len - N来表示正向被删除元素所在的位置。

代码:

  1. public ListNode removeNthFromEnd(ListNode head, int n) {
  2. if(head == null) return null;
  3.  
  4. ListNode help = head;
  5. int length = 1;
  6. while(help.next != null){
  7. length++;
  8. help = help.next;
  9. }
  10.  
  11. if(n == length){
  12. head = head.next;
  13. }else{
  14. ListNode temp = head;
  15. for(int i = 1 ; i < length - n ; i++) temp = temp.next;
  16.  
  17. if(n == 1) temp.next = null;
  18. else temp.next = temp.next.next;
  19. }
  20. return head;
  21. }

这样的算法有个很低效的地方,如果我要删除一个very very 长的链表的倒数第二个元素,我得先遍历一遍,这一步可能半小时过去了。然后我又得查找一遍,才能删除。

网络上的思路非常精辟,双指针(快指针 & 慢指针),虽然原理还没有很理解,但是举例这样的确能得到正确答案。快指针比慢指针先走N步,N步以后快慢指针同时继续走,当快指针走到链表尾部时,慢指针指向的就是被删除元素的前一个元素。精妙的思路。

  1. public ListNode removeNthFromEnd(ListNode head, int n) {
  2. if(head == null) return null;
  3.  
  4. ListNode fast = head;
  5. ListNode slow = head;
  6.  
  7. int fastGo = n;
  8. while(n > 0 && fast.next != null){
  9. fast = fast.next;
  10. n--;
  11. }
  12.  
  13. if( n == 0 ){
  14. while(fast.next != null){
  15. fast = fast.next;
  16. slow = slow.next;
  17. }
  18. if(slow.next.next != null) slow.next = slow.next.next;
  19. else slow.next = null;
  20. }else{
  21. head = head.next;
  22. }
  23. return head;
    }

[leetcode]_Remove Nth Node From End of List的更多相关文章

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

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

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

  3. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

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

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

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

  7. LeetCode OJ-- Remove Nth Node From End of List

    https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...

  8. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  9. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

随机推荐

  1. [AIR] AS3.0设置屏保功能

    package com.controls { import flash.desktop.NativeApplication; import flash.events.Event; import fla ...

  2. ubuntu 命令行软件包管理

    安装软件包 apt-get install vim 卸载 apt-get remove/autoremove vim autoremove 会顺便卸载未使用,未被依赖的软件包 查询或者搜索软件包:: ...

  3. Codeforces 622F 「数学数论」「数学规律」

    题意: 给定n和k,求 1 ≤ n ≤ 109, 0 ≤ k ≤ 106 思路: 题目中给的提示是对于给定的k我们可以求出一个最高次为k+1的关于n的通项公式. 根据拉格郎日插值法,我们可以通过k+2 ...

  4. 范式(Oracle)

    三范式 ------------数据库的三范式-------------- (1).要有主键,列不可分 (2).不能存在部分依赖:当有多个字段联合起来作为主键的时候,不是主键的字段不能部分依赖于主键中 ...

  5. 【转】开源性能测试工具 - Apache ab 介绍

    版权声明:本文可以被转载,但是在未经本人许可前,不得用于任何商业用途或其他以盈利为目的的用途.本人保留对本文的一切权利.如需转载,请在转载是保留此版权声明,并保证本文的完整性.也请转贴者理解创作的辛劳 ...

  6. Ibatis 异常:Unable to open connection to "oledb , provider V2.0.0.0 in framework .NET V2.0".

    在实际项目中使用了ibatis,然后在开发过程中遇到一些问题,最严重的就是这个“Unable to open connection to "Microsoft SQL Server, pro ...

  7. char nchar varchar nvarchar的区别(转)

    char nchar  varchar nvarchar的区别   今天在论坛里看到有人激烈讨论这几个数据类型的区别跟实际使用情况,很多人都搞不清楚究竟哪个场景使用哪个数据类型 现在就摘录一下sql2 ...

  8. 初探接口测试框架--python系列2

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  9. python实现批量ping IP,并将结果写入

    最近工作需要,写了一个Python小脚本,分享给大家,因为公司的IP用的差不多了,然后离职人员的IP有没有及时删除,导致没多少IP用了,所以做了一个python脚本跑了跑,清出来一堆ping不通的IP ...

  10. EF,ADO.NET Entity Data Model简要的笔记

    1. 新建一个项目,添加一个ADO.NET Entity Data Model的文件,此文件会生成所有的数据对象模型,如果是用vs2012生的话,在.Designer.cs里会出现“// Defaul ...