• 作者: 负雪明烛
  • 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:

  1. 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 个结点的更多相关文章

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

  2. 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

  3. [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 ...

  4. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

  6. 019 Remove Nth Node From End of List 删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...

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

  8. 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)

    题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了   2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...

  9. Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

随机推荐

  1. urllib的基本使用介绍

    1. urllib中urlopen的基本使用介绍 1 ### urllib中urlopen的基本使用介绍 2 3 ## urlopen的基本用法(GET请求) 4 import urllib.requ ...

  2. c语言转义字符如下

    #define MQTT_EVENT_REPORT_BOX_STATUS_FORMAT "{"\                                           ...

  3. HTTP请求 Java API

    1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...

  4. 如何让Linux 机器CPU使用率变高

    如何让Linux 机器CPU使用率变高 一.实现 1.单行命令搞定 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" ...

  5. OC Swift 走马灯效果

    我们常见走马灯样式的功能,下面整理一下 Object-C 与 Swift 的实现代码 OC UILabel *label3 = [[UILabel alloc] initWithFrame:CGRec ...

  6. Linux学习 - 文本编辑器Vim

    一.Vim工作模式 二.命令 插入 a 光标后插入 A 光标所在行尾插入 i 光标前插入 I 光标所在行首插入 o 光标下插入新行 O 光标上插入新行   删除 x 删除光标处字符 nx 删除光标处后 ...

  7. java设计模式—Decorator装饰者模式

    一.装饰者模式 1.定义及作用 该模式以对客户端透明的方式扩展对象的功能. 2.涉及角色      抽象构件角色:定义一个抽象接口,来规范准备附加功能的类. 具体构件角色:将要被附加功能的类,实现抽象 ...

  8. C# .exe和.dll文件图标资源提取工具

    Windows 可执行文件(.exe)和动态库文件(.dll)图标资源提取工具 GitHub 功能 图标资源预览 图标资源导出(仅支持导出 PNG 格式) 代码 获取图标资源使用了 Win32 API ...

  9. 转:Java多线程基础问题

    如果你即将去一家从事大型系统研发的公司进行Java面试,不可避免的会有多线程相关的问题.下面是一些针对初学者或者新手的问题,如果你已经具备良好的基础,那么你可以跳过本文,直接尝试针对进阶水平的Java ...

  10. WHUCTF PWN题目

    花了大概两天时间来做WHUCTF的题目,第一次排名这么靠前.首先感谢武汉大学举办这次萌新赛,也感谢fmyy的师傅的耐心指导,让我第一次做出堆的题目来. pwnpwnpwn 这是一道栈题目,32位程序, ...