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 return its head.
翻译:给定一个链表,删除倒数第n个节点并返回其头部。
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.
Note:
给出的n一定是有效的。
我的思路:数据结构——因为要倒着数,所以考虑用栈;
算法——用while循环将链表节点按顺序全部存入栈中,然后再对n进行while循环自减,直到n==1,退出循环,此时栈顶就是要删除的节点,用一个节点del存储它,再弹出一个就是它的前驱节点pre。pre.next = del.next;这就是删除节点。
我的代码:
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head==null || n < 1) {
return null;
}
Stack<ListNode> st = new Stack<ListNode>();
ListNode cur = head;
while (cur != null) {
st.push(cur);
cur = cur.next;
}
while (n-- != 1) {
st.pop();
}
ListNode del = st.pop();
if (st.size() == 0) { // 此时栈如果空了,那么删除的就是最开始的头结点,这时候直接返回del.next 即可。
return del.next;
}
ListNode pre = st.pop();
pre.next = del.next;
del = null;
return head;
}
我的复杂度:O(N + n) N为总长度,n为倒数个数
编码过程中的问题:
1、一开始没考虑到要删的del没有前驱的情况,得到del时,栈就空了,再pop()就报错了:EmptyStackException 错误用例:[1],1。
参考答案代码:
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode newHead = new ListNode(0); // 因为可能要删除的就是head所以起一个头结点 0
newHead.next = head;
ListNode pre = newHead; // 慢指针
ListNode fast = newHead; // 快指针
while(n-- > 0){
fast = fast.next; // 初始,快指针与慢指针相差n
}
while(fast.next!=null){
fast = fast.next;
pre=pre.next;
}
pre.next = pre.next.next;
return newHead.next;
}
参考答案复杂度:O(N + n) 不过它的空间复杂度是O(1) ,我写的那个空间复杂度O(N)
答案思路:利用快慢指针(其实叫前后指针更贴切)的概念,初始时快慢指针之间的距离为n,然后同速前进,当快指针的next==null的时候(注意不是快指针自己),此时慢指针的next必然指向倒数第n个。【因为可能要删除的就是head所以起一个头结点 0 】
此处就不需要del的判断了,所以直接 pre.next = pre.next.next; 。(ps:其实我都忘了还可以这么删除,啊哈哈哈)
LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)的更多相关文章
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- 019 Remove Nth Node From End of List 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...
- [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了 2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)
Given a linked list, remove the n-th node from the end of list and return its head. Example: ...
随机推荐
- java File delete() 失败,又没有报错。
因为该文件流还没关闭,就执行了delete(),所以删除失败. 先举几个可以删除掉文件和删除不掉文件的例子(先在F盘创建test1.txt文件,然后可以直接拷贝代码到IDE执行),最后总结下原因: 例 ...
- tagName()方法详解
1.该方法可以通过元素的标签名称来查找元素.这个方法搜索到的元素通常不止一个,所以一般建议结合使用findElements方法来使用: driver.findElement(By.xpath(&quo ...
- pandas数据分析第二天
一:汇总和计算描述统计 pandas对象拥有一组常用的数据和统计方法,用于从Series中提取单个值(sum,mean)或者从DataFrame的行或者列中提取一个Series对应的Numpy数组方法 ...
- django使用celery实现异步操作
需求: django支持的http请求都是同步的,对于需要耗时较长的操作可能会导致阻塞.为此我们需要引入异步处理机制,即收到客户端请求后立即给予响应,具体任务交给另一个进程处理. 使用方法: 1. 安 ...
- Angular4中常用管道(转载)
Angular4中常用管道 通常我们需要使用管道实现对数据的格式化,Angular4中的管道和之前有了一些变化,下面说一些常用的管道. 一.大小写转换管道 uppercase将字符串转换为大写 low ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A - Neverending competitions
地址:http://codeforces.com/contest/765/problem/A 题目: A. Neverending competitions time limit per test 2 ...
- cdoj1328卿学姐与诡异村庄
地址:http://acm.uestc.edu.cn/#/problem/show/1328 题目: 卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others) ...
- maven项目中如何创建web.xml
在web工程创建时创建web.xml:用Eclipse新建一个web项目没有自动生成web.xml 在创建maven工程时,大多没有创建web.xml文件,web.xml路径:src/main/web ...
- hadoop20---代理另一种方式
package cn.itcast_05_proxy.service; /** * 这是一个业务的接口,这个接口中的业务就是返回衣服的价格 */ public interface IBoss {//接 ...
- Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx. 问题: Linux ...