题目: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. springboot 整合 rabbitmq

    http://blog.720ui.com/2017/springboot_06_mq_rabbitmq/

  2. informix-時間格式的各種用法

    以下是我在網路上所收集到的關於informix 時間的sql函數用法,有在使用informix資料庫的人,可以參考看看囉! today,返回現在系統日期 current 返回現在日期含時間,相當於sq ...

  3. Go语言核心之美 4.3-多返回值

    在Go语言中.函数能够有多个返回值,这个特性我们已经在之前的样例见过非常多,非常多标准库函数都会返回两个值,一个是期望得到的函数执行结果,另外一个是函数出错时的错误值. 以下的程序是findlinks ...

  4. [sh]shell脚本栗子

    我会定期的把看到的一些好的shell和py脚本搜集在这里,供参考学习: 命令行回收站 推荐一个不相关的:trash-cli,就是命令行版的回收站,它的神奇之处在于不是简单的把文件移动到回收站,而且可以 ...

  5. NYOJ 467 中缀式变后缀式

    做了表达式求值那道题之后做的 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更"习惯于"后 ...

  6. python内置函数之dict()

    class dict(**kwargs) 返回一个字典.本方法用来创建一个字典对象.只能传入一个参数. >>> dict(a=1) {'a': 1} 也可以传入映射函数作为参数 &g ...

  7. php chr() ord()中文截取乱码问题解决方法

    今天看到chr() ord()中文截取乱码问题这个例子,觉得相当的不错,拿出来和大家分享下,有兴趣的朋友可以去试下,看看怎么样. 代码如下: <?php $lenth = ; $str = &q ...

  8. apue编程之参考df代码写的一个简单的df命令的源代码

    代码: #include <stdio.h> #include <mntent.h> #include <string.h> #include <sys/vf ...

  9. JS学习笔记(2)--正则表达式获取指定字符串

    js 正则提取字串 这里就有:SA 怎么用正则提取sa出来 var str=“这里就有:SA ”怎么用正则提取sa出来 YDhcui | 浏览 2087 次 推荐于2016-05-30 18:25:4 ...

  10. CSS布局奇淫技巧之--各种居中<转>

    居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...