问题:

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.

官方难度:

Easy

翻译:

给定一个链表,删除其中倒数第n个节点,方法返回头节点。

例子:

链表: 1->2->3->4->5,n=2。

删除节点后的链表:1->2->3->5。

  1. 本题的节点定义,同No.002(Add Two Numbers)相同。
  2. 首先需要确定,给定链表的长度,之后定位待删除的节点,和此节点的前一个节点。由于是原生态的链表,没有现成的定位方法,所以为了提高效率,在确定链表长度的同时,将节点放入容器中,以便之后获取节点。
  3. 需要考虑删除第一个节点的特殊情况,此时没有上一个节点。
  4. 将失去引用的节点,即删除的节点置null,防止可能的内存泄漏。
  5. 最后剩下一个问题,使用什么容器合适?HashMap和ArrayList在性能上要比LinkedList要高,HashMap空间占用更大,所以应该是用ArrayList比较好。不过ArrayList是基于数组实现的,所以我也尝试着使用一个长度不断扩张的数组来实现。
  6. 最后入参检查,对n的检查放在确定链表长度之后。

解题代码:

     public static ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) {
throw new IllegalArgumentException("Input error");
}
ListNode current = head;
ListNode[] array = new ListNode[] {};
int length = 1;
while (current != null) {
array = extendArray(array, current);
current = current.next;
length++;
}
if (n > length) {
throw new IllegalArgumentException("Input error");
}
// 记录删除节点,以及上一个节点
int index = length - n;
int lastIndex = length - n - 1;
// 删除第一个节点的情况
if (lastIndex == 0) {
head = head.next;
} else {
ListNode deleteNode = array[index - 1];
ListNode lastNode = array[lastIndex - 1];
lastNode.next = deleteNode.next;
deleteNode = null;
}
return head;
} private static ListNode[] extendArray(ListNode[] array, ListNode node) {
ListNode[] listArray = new ListNode[array.length + 1];
System.arraycopy(array, 0, listArray, 0, array.length);
listArray[array.length] = node;
return listArray;
} private static class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
}

removeNthFromEnd

相关链接:

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q019.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

No.019:Remove Nth Node From End of List的更多相关文章

  1. 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 ...

  2. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  3. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  4. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

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

  5. lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

    题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...

  6. LeetCode 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

  7. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

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

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

随机推荐

  1. 使用Sublime Text 2 编辑Markdown

    http://www.ituring.com.cn/article/6815 一.安装 下载Sublime Text 2 安装 二.安装Package Control 按Ctrl + ` 打开cons ...

  2. MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version=" ...

  3. MSSQL Server数据库的四种连接方法和sql连接字符串

    MSSQL Server数据库的四种连接方法和sql连接字符串 分类: [ 03 ] C#(131) [ 07 ] SQL Server(68) [ 01 ] .NET(189) 今天用SQL Ser ...

  4. JDBC操作数据库,第一:jsp插入mysql数据库,坎坷摸索分享

    JSP连接数据库,坎坷摸索了好久,现在终于做好了,分享一下,希望对更多热爱编程学习的人有所帮助!!!谢谢 第一:首先准备的就是已经安装好Mysql,这里不做多叙述,百度可以做到. 然后在mysql数据 ...

  5. 关于页面查询多数据查询问题(foreach)

    最近纠结的一个问题,就是页面综合查询总报错,之前用过传参用list传就没问题,但现在用map总是报错,缓释直接贴图吧,希望对遇到问题的朋友有帮助页面传来参数,之前是 这样写的,直接将拿来的数据封装成一 ...

  6. MySQL(三) 数据库表的查询操作【重要】

    序言 1.MySQL表操作(创建表,查询表结构,更改表字段等), 2.MySQL的数据类型(CHAR.VARCHAR.BLOB,等), 本节比较重要,对数据表数据进行查询操作,其中可能大家不熟悉的就对 ...

  7. 快速入门系列--CLR--01基本概念

    在.NET平台用C#这么久,自然会发现其版本很多,相应的概念也会很多,常常都是萌萌哒.而在实际工作中经常会遇到需要配置dll版本号,公钥token等场景,因而对C#.NET.CLR.框架类型等基础概念 ...

  8. java JFileChooser选择文件和保存文件

    //文件过滤器import java.io.File; import javax.swing.filechooser.FileFilter; public class MyFilter extends ...

  9. 数据结构:C_顺序栈的实现

    数据结构顺序栈的实现(C语言版) 1.写在前面 栈是一种遵循元素先进(Push)后出(Pop)规则的线性表,它的实现可以用数组或者链表. ..... 2.代码分解 2.1对栈的结构定义: typede ...

  10. nodejs 调试 node-inspector包

    nodejs  调试调试比较麻烦,让习惯了用chrome浏览器调试的前端同学来说有点不适用  node-inspector这个包让我们可以在chrome上像调试前端代码一样来调试nodejs 1.全局 ...