No.019: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,
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。
- 本题的节点定义,同No.002(Add Two Numbers)相同。
- 首先需要确定,给定链表的长度,之后定位待删除的节点,和此节点的前一个节点。由于是原生态的链表,没有现成的定位方法,所以为了提高效率,在确定链表长度的同时,将节点放入容器中,以便之后获取节点。
- 需要考虑删除第一个节点的特殊情况,此时没有上一个节点。
- 将失去引用的节点,即删除的节点置null,防止可能的内存泄漏。
- 最后剩下一个问题,使用什么容器合适?HashMap和ArrayList在性能上要比LinkedList要高,HashMap空间占用更大,所以应该是用ArrayList比较好。不过ArrayList是基于数组实现的,所以我也尝试着使用一个长度不断扩张的数组来实现。
- 最后入参检查,对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/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.019:Remove Nth Node From End of List的更多相关文章
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- 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 ...
- 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 ...
- 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 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
随机推荐
- Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范
Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范 需要增加的jsr规范1 开发常用的10个规范(jsp etc)1 other开发常用的50个规范1 需要 ...
- KnockoutJS 3.X API 第四章 表单绑定(7) event绑定
目的 event绑定即为事件绑定,即当触发相关DOM事件的时候回调函数.例如keypress,mouseover或者mouseout等 例如: Mouse over me Details var vi ...
- 总结baiduTemplate和djangoTemplate的学习
引入 开发工作中需要,除了今天要介绍的两种template,还有很多模板,但他们的终点都是相同的,都是为了开发的便利. 模板的作用 是一套模板语法,开发者可以通过写一 ...
- StringUtils 的用法
1.public static boolean isEmpty(String str) 判断某字符串是否为empty,标准是 null == str 或 str.length() == 0 2.pub ...
- CSS多列布局
× 目录 [1]列宽 [2]列数 [3]列间距[4]列rule[5]跨列[6]列填充[7]多列 前面的话 CSS新增了多列布局特性,可以让浏览器确定何时结束一列和开始下一列,无需任何额外的标记.简单来 ...
- Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成
与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...
- log4net 中错误 System.Web.HttpException (0x80004005): 文件不存在
用日志组件,Global 中配置的输出最后一个错误信息,总是出现下面的错误信息: 2014-04-01 14:35:41,757 级别:ERROR 信息:[Exception]:System.Web. ...
- CentOS7 PostgreSQL安装
CentOS7 PostgreSQL安装 CentOS7 PostgreSQL安装 Install 安装 使用yum安装 yum install http://yum.postgresql.org/9 ...
- Elasticsearch 管理文档
ES支持近实时的索引.更新.查询.删除文档,近实时就意味着刚刚索引的数据需要1秒钟后才能搜索到,这也是与传统的SQL数据库不同的地方. 更多的ES文档资料参考:Elasticsearch官方文档翻译 ...
- placeholder的兼容处理(jQuery下)
这是一个老问题,结合前辈们的经验,需要处理的问题有一下几个. 1.只有输入框(input/textarea)下的palaceholder属性存在的时候才需要处理这类兼容 2.处理好输入框上焦点和是焦点 ...