题目:移除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. topngroupcollector

    分类的字段int f1 /* key: f1 * value: doc * size: top n */ map<int, doc>() if(map.size==n) buildOrde ...

  2. java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是 byte  short int long 其中byte只有一个字节 0或1,在此不详细讲解. 其他的三种类型如下: 1.基本类型:short 二进制位数:16包装 ...

  3. java的String类(一)

    final类,无子类. 类内定义了char数组value[],私有,不可修改. String的长度,length(). 判空,isEmpty(). 索引index处的字符,charAt(index). ...

  4. 在页面中使用Eval的两种方案

    ①直接取<%#Eval("name")%> ②进行运算<%#Convert.ToInt32(Eval("count"))-(Eval(&quo ...

  5. 将assembly包添加到自己的maven仓库

    mvn install:install-file -DgroupId=com.asiainfo -DartifactId=spark-assembly -Dversion=1.2.0 -Dpackag ...

  6. Begin using git (Part1) - Git的安装与配置

    Git提供了适用于Linux, Windows, OSX的客户端, 本节以Windows为例介绍基本安装与配置. 所需工具:msysgit, kdiff3. Get windows installer ...

  7. NAT学习笔记

    NAT介绍 NAT, 全称网络地址转换(Network Address Translation),是一种在IP封包通过路由器或防火墙时重写来源IP地址或目的IP地址的技术. NAT的分类及介绍 NAT ...

  8. 慕课网-安卓工程师初养成-4-7 Java循环语句之 while

    来源: http://www.imooc.com/code/1420 生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 ...

  9. Swift学习(二)

    一.方法 在OC中,函数是C语言的形式,跟方法不一样 函数:int sum (int num1, int num2) { return num1 + num2;   } 方法:- (int)sum:( ...

  10. awk中怎么比较字符串??

    awk -vOFS="_" '{print $1,$2,$3} http://bbs.chinaunix.net/thread-1099655-1-1.html