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

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

代码:

public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null; ListNode help = head;
int length = 1;
while(help.next != null){
length++;
help = help.next;
} if(n == length){
head = head.next;
}else{
ListNode temp = head;
for(int i = 1 ; i < length - n ; i++) temp = temp.next; if(n == 1) temp.next = null;
else temp.next = temp.next.next;
}
return head;
}

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

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

public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null; ListNode fast = head;
ListNode slow = head; int fastGo = n;
while(n > 0 && fast.next != null){
fast = fast.next;
n--;
} if( n == 0 ){
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
if(slow.next.next != null) slow.next = slow.next.next;
else slow.next = null;
}else{
head = head.next;
}
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. MYSQL C API : mysql_real_query()

    enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, MYSQ ...

  2. linux 去掉 ^M

    要去除他,最简单用下面的命令: dos2unix filename 亲测可用   以下方式不可以: set ff=unix  %s/^M//g 可能是^M输入方式有问题 ^M 输入方法: ctrl+V ...

  3. 解决Web部署 svg/woff/woff2字体 404错误(转)

    http://blog.sina.com.cn/s/blog_4997f1b90102vkjn.html 最近项目中用到了fontawesome-webfont.svg等字体.部署项目后,发现没有&l ...

  4. svn: E175002: can not read HTTP status line

    问题:eclipse连接svn:https://bdsvn-pc/svn/Project,报错svn: E175002: can not read HTTP status line 解决办法:将域名改 ...

  5. ArcGIS Server建立缓存(切图)原理解析[图解] (转载)

    GoogleMap ,VirtualEarth ,YahooMap 等,目前所有的WebGIS都使用了缓存机制 以提高地图访问速度.原理都是将地图设定为多个比例尺,对于每个比例尺提前将地图分成若干小图 ...

  6. codeforces 2B The least round way 【DP】

    VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...

  7. Windows程序设计-窗口和消息

    本章内容比较少,没有代码类的内容,这里付思维导图一章,看起来比较清晰.

  8. Oracle教程:如何诊断节点重启问题(转载)

    本文对如何诊断RAC环境中节点重启问题进行了介绍.适用于10gR2和11gR1. 首先我们对能够导致节点重启的CRS进程进行介绍.1.ocssd : 它的主要功能是节点监控(Node Monitori ...

  9. 开发者眼中最好的 22 款 GUI 测试工具

    1.Abbot - Java GUI 测试框架 Abbot是一个基于GUI的简单的Java测试框架,它能够帮助开发者测试Java用户界面. 它提供事件自动生成和验证Java GUI组件,使您能够轻松地 ...

  10. 置换贴图 Displacement Mapping

    视差贴图和法线贴图都是使用特定的手段来达到欺骗视觉的目的,让人以为物体的表面是凹凸起伏的.而置换贴图却是真的将模型的顶点进行偏移,在原本的平面上创造出凹凸的效果.既然是对顶点进行偏移,那么就需要模型有 ...