【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
- 个人公众号:负雪明烛
- 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣,Python, C++, Java
题目地址:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
题目描述
Given a linked list, remove the n-th node from the end of list and return its head.
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:
- Given n will always be valid.
Follow up:
- Could you do this in one pass?
题目大意
删除一个单链表的倒数第n个节点。
解题方法
双指针
先来分析一下这个题埋的坑吧。
第一,首先要判断这个n是不是有效,如果n超出链表长度怎么办,还好题目给了n是有效的。
第二,如果要删除头结点怎么办?估计很多人栽在了这个上面。
第三,题目说的是单链表没错,但是是否有环呢?当有环的时候,没有倒数第n个节点,你让我怎么办?很遗憾,题目没有说明这一点,我认为这是题目不严谨的地方。
具体到解法,这个题肯定是使用快慢指针啊,两个之间的距离是n,所以当快指针指向结尾的时候,慢指针正好指向了倒数第n个。因为要删除慢指针的位置,所以需要一个pre指针记录一下前面的那个节点的位置。
由于有可能删除首节点,所以使用哑结点当做新的头可以解决。
Python代码如下:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
root = ListNode(0)
root.next = head
fast, slow, pre = root, root, root
while n - 1:
fast = fast.next
n -= 1
while fast.next:
fast = fast.next
pre = slow
slow = slow.next
pre.next = slow.next
return root.next
C++代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* prev = dummy;
ListNode* cur = dummy;
while (n--) {
cur = cur->next;
}
while (cur && cur->next) {
cur = cur->next;
prev = prev->next;
}
prev->next = prev->next->next;
return dummy->next;
}
};
日期
2018 年 6 月 23 日 —— 美好的周末要从刷题开始
2019 年 1 月 10 日 —— 加油
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点的更多相关文章
- [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个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- [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个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 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(链表)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了 2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
随机推荐
- 巩固javaweb第十五天
巩固内容: 单选按钮: 在注册功能中,用户选择学历使用的是单选按钮,并且是多个单选按钮,每个选项对 应一个单选按钮,用户只能选择其中一个,这多个单选按钮的格式相同.如果用户要输入 的信息只有少数几种可 ...
- pow()是如何实现的?
如1.5 ** 2.5,如何计算?似乎是这样的: 1. cmath calculates pow(a,b) by performing exp(b * log(a)). stackoverflow 2 ...
- 【leetcode】222. Count Complete Tree Nodes(完全二叉树)
Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...
- linux 配置本地yum
1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...
- 用Myclipse开发Spring(转)
原文链接地址是:http://www.cnitblog.com/gavinkin555/articles/35973.html 1 新建一个项目 File----->New ----->P ...
- Spring MVC入门(二)—— URI Builder模式
URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...
- springboot整合jetty
1.jetty介绍 通常我们进行Java Web项目开发,必须要选择一种服务器来部署并运行Java应用程序,Tomcat和Jetty作为目前全球范围内最著名的两款开源servlet容器,该怎么选呢. ...
- 数据库系统相关SQL
杀进程 查出所有被锁住的表 select b.owner TABLEOWNER, b.object_name TABLENAME, c.OSUSER LOCKBY, c.USERNAME LOGINI ...
- Spring 与 SpringBoot 的区别
概述 Spring 与 SpringBoot 有什么区别???梳理一下 Spring 和 SpringBoot 到底有什么区别,从 Spring 和 SpringBoot 两方面入手. Spring ...
- linux重启后JDk环境变量配置失效最终解决方案
最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端 ...