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

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

就是让你删除单链表倒数第n个节点,同时希望能只遍历一次。

解法一: 如果不遍历完所有节点,怎么知道倒数第n个在哪里呢?但是遍历完成以后,删除不是又需要遍历么?不过删除一个node实际上只需要一个pre node就行了。于是,考虑使用一个HashMap保存遍历过的节点,实现删除。

 public ListNode removeNthFromEnd(ListNode head, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(head == null) return null;
HashMap<Integer, ListNode> map = new HashMap<Integer, ListNode>();
int i = 0;
ListNode p = head;
while(p != null){
map.put(++i, p);
p = p.next;
}
ListNode pre = map.get(i - n);//i - n is the node that right BEFORE the to-be-deleted node.
if(pre != null){
ListNode deleted = pre.next;
if(deleted != null)
pre.next = deleted.next;
}else {//if pre is null, means we are deleting head
head = head.next;
}
return head;
}

HashSet deletion

感觉每次做题,如果用到了额外的数据结构或者空间,都有一种开挂的感觉。能不能不显式使用额外的空间或者数据结构呢?想到最近看了Algorithm 4th edtion关于递归删除BST的方法,有一些启发。

解法二: 我们尝试使用递归来解。

 public ListNode removeNthFromEnd2(ListNode head, int n) {
if(head == null) return null;
int[] counter = new int[]{0};
return removeNthFromEnd2(head,n,counter);
} private static ListNode removeNthFromEnd2(ListNode head, int n, int[] counter){
if(head.next == null){// we reached tail
counter[0] = 1;
}else {
head.next = removeNthFromEnd2(head.next, n, counter);
counter[0]++;//we increment this counter to record when we back from the recursive, namely back from the last node
}
if(counter[0] == n){//oh, this is nth node backward, we just RETURN THE NEXT NODE.
return head.next;
}else return head;//otherwise return whatever we received
}

recursive deletion

是不是看上去很简洁?同样的,我们还是需要遍历完所有节点。但是递归的好处是每次返回的都是“backward",通过这个特点,我们使用一个counter变量,在递归返回的时候,记录返回经过的节点(Java不如C#,没有ref参数,所以只能用一个数组记录这个counter变量以达到引用的效果)。这样当counter到打n的时候,我们知道,哦,好啦这个当前节点就是我们要删除的节点咯。所以,instead of直接返回head节点,我们返回删除节点的next,这样,在递归调用再次返回的时候,pre的next就指向了删除节点的next咯。

好吧,承认实际上这也是遍历了两次链表。。。:<

解法三: 为了减少遍历,我们还可以用两个指针slow和fast,fast先走n步。then,slow和fast共同进步直到fast走到尾巴。Code在这里

LeetCode 笔记系列四 Remove Nth Node From End of List的更多相关文章

  1. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  2. leetcode第19题--Remove Nth Node From End of List

    Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  3. 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 ex ...

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

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

  6. 【leetcode❤python】 19. Remove Nth Node From End of List

    #-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...

  7. leetcode个人题解——#19 Remove Nth Node From End of List

    思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...

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

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

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

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

随机推荐

  1. shell脚本分析 nginx日志访问次数最多及最耗时的页面

    当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...

  2. Atitit.软件仪表盘(0)--软件的子系统体系说明

    Atitit.软件仪表盘(0)--软件的子系统体系说明 1. 温度检测报警子系统 2. Os子系统 3. Vm子系统 4. Platform,业务系统子系统 5. Db数据库子系统 6. 通讯子系统 ...

  3. .atitit.web 推送实现解决方案集合(3)----dwr3 Reverse Ajax

    .atitit.web 推送实现解决方案集合(3)----dwr3 Reverse Ajax 1. 原理实现 1 2. Page  增加配置,增加回调函数dwr.engine.setActiveRev ...

  4. vue-cli 中实现简单动画效果 (vue2.0)

    1,写一个简单的headcomp组件如下: <template> <div class="box"> <transition name="m ...

  5. EAV/ESS 8.x 自定义服务器正确方法+更新服务器列表

    下面用64位的ESET Smart Security 8.0.319.1进行设置自定义更新服务器说明(注:修改方法32位和64位通用) 1.  让我们先看一下ESET Smart Security 8 ...

  6. malloc,我误解你了

    malloc用于动态申请内存,这个学过C语言的都知道.忘记了在哪本书上看到,malloc申请的内存不一定是连续,于是一直记住了.这句话有错吗?没有!但是当时只是记住了这个知识点,而没有深入的思考.直到 ...

  7. FreeRtos——多任务

    官方资料整理测试: 多任务和单任务几乎没有差别.只用多创建一个或多个任务,其他地方和单任务时相同. static void AppTaskCreate(void) { xTaskCreate(vTas ...

  8. hdu1331(记忆化搜索)

    #include<iostream> #include<stdio.h> #include<string.h> using namespace std; typed ...

  9. 从文件/文件流的头字节中得到mime信息

    在写网络爬虫的时候,需要根据链接来获取文件类型,将内容正确存储.之前我都是根据链接的后缀来判断的,比如: http://img12.360buyimg.com/da/20120330/88_31_Zy ...

  10. 在Asp.net中为图像加入水印信息

    using System.Drawing; using System.IO; using System.Drawing.Imaging; private void AddTextToImg(strin ...