题目:移除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. PreparedStatement批量(batch)插入数据

    JDBC操作数据库的时候,需要一次性插入大量的数据的时候,如果每次只执行一条SQL语句,效率可能会比较低.这时可以使用batch操作,每次批量执行SQL语句,调高效率. public Boolean ...

  2. XXX项目 android 开发笔记

    1 工具? eclipse or android studio fragment 套用

  3. 蓝桥杯---汉字取首字母(位运算 & 水题)

    确实题目虽然有点水,但是开始的时候好像还真的没有想到怎么提取出这个编号一不小心感觉可以可以用unsigned char 这种类型,直接转为16进制,但是之后发现虽然第一次在codeblock中还行,但 ...

  4. 自定义View的基本流程

    1.明确需求,确定你想实现的效果2.确定是使用组合控件的形式还是全新自定义的形式,组合控件即使用多个系统控件来合成一个新控件,你比如titilebar,这种形式相对简单,参考:http://blog. ...

  5. V$SGA_RESIZE_OPS.STATUS = ERROR, and MMAN / auto-tuning stops.

    Oracle Server - Standard Edition - Version: 10.2.0.3This problem can occur on any platform. SYMPTOMS ...

  6. 在虚拟机上安装红帽Linux.5.5.for.x86.服务器版系统(按针对安装oracle 10g作的配置)

    在虚拟机上安装红帽Linux.5.5.for.x86.服务器版系统(按针对安装oracle 10g作的配置)   软件版本: 虚拟机版本:vmwareworkstation 7.11 build-28 ...

  7. 一步一步学习Unity3d学习笔记系1.4单服模式架构

    单服模式更适合做手游,只有一个服务器,在程序中通过代码模块来实现各功能,而不是物理模块划分. 登录模块实现,账号数据处理, 用户模块,处理角色权限处理, 匹配模块,匹配战斗 好友模块,负责好友管理 战 ...

  8. OpenStack和Redis

    前言: 最近开始捣鼓OpenStack了,在用RDO部署OpenStack的时候,发现装了Redis, 遂决定看看OpenStack哪些地方(可以)用到Redis.  Redis作为OpenStack ...

  9. Class attributes

    In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...

  10. [vsftp]500 OOPS: cannot change directory

    这个报错需要检查 1./etc/passwd 用户的主目录 2./etc/vsftpd/vuser_conf 下每个用户的local_root 3.每个用户目录给ftpuser加上rwx权限,一定要有 ...