题意:删除链表中倒数第N个节点。

法一:递归。每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点;否则的话,问题转化为子问题“对head->next这个链表删除倒数第N个节点”,将head的next指针指向该子问题的结果,返回head即可。这个方法时间复杂度高,不推荐。

/**
* 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) {
if(head == NULL) return NULL;
int cnt = 0;
ListNode *tmp = head;
while(tmp){
++cnt;
tmp = tmp -> next;
}
if(cnt == n){
return head -> next;
}
head -> next = removeNthFromEnd(head -> next, n);
return head;
}
};

法二:快慢指针,快指针先比慢指针多走N步,然后两者同时走,这样的话,当快指针的next指向NULL时,说明慢指针的next指向要被删除的节点。

注意:fast先走n步后,如果为NULL,表示链表长为n,则直接删除头结点,返回head->next即可。

/**
* 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* fast = head;
ListNode* slow = head;
while(n--){
fast = fast -> next;
}
if(fast == NULL) return head -> next;
while(fast -> next){
fast = fast -> next;
slow = slow -> next;
}
slow -> next = slow -> next -> next;
return head;
}
};

 

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. lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

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

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

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

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

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

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

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

  6. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

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

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

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

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

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

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

随机推荐

  1. Spring错误:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bi

    在使用SSM框架传递多个参数的时候发生如下错误: 原因是因为在传递多个参数的时候没有使用注解@Param,所以才包如下错误: 参考的技术文章:https://blog.csdn.net/sinat_2 ...

  2. INCA二次开发-INCACOM

    1.INCA介绍 INCA是常用的汽车ECU测试和标定的,广泛应用于动力总成等领域.INCA提供了丰富的接口,供用户自动化.定制化.本公众号通过几篇文章,介绍下一些二次开发的方法,本篇介绍INCA-C ...

  3. MongoDB - 用户名密码认证

    参考 offical doc medium Mongo roles说明 https://docs.mongodb.com/manual/reference/built-in-roles/#userAd ...

  4. mac系统,docker下载安装

    1. docker安装文档 2. Docker下安装SQL Server

  5. 消息队列(五)--- RocketMQ-消息存储4

    问题 index 文件有什么作用,结构又是如何 概述 index 文件主要是为了 message key 服务的,rocketmq 发送消息的时候可以带上 key , messge key 是为了标识 ...

  6. 【PAT甲级】1069 The Black Hole of Numbers (20 分)

    题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...

  7. Java数组和方法

    1. 数组可以作为方法的参数 package cn.itcast.day05.demo04; /* 数组可以作为方法的参数. 当调用方法的时候,向方法的小括号进行传参,传递进去的其实是数组的地址值. ...

  8. 【C语言】(数组方式)输出一组成绩中的最高分与最低分

    两种不同方式获取最大值与最小值 代码1: #include <stdio.h> int main() { ], sum = , max, min; int i; printf(" ...

  9. 传奇脚本:#AutoRun NPC SEC 参数说明

    传奇脚本:#AutoRun NPC SEC 参数说明 SEC:按秒运行MIN:按分运行HOUR:按小时运行DAY:按天运行RunOnDay:按每天什么时候运行RUNONWEEK:按星期几及时间运行机器 ...

  10. WCF技术归纳

    本人在2013年就做过一个WCF的项目,但最近又开始看相关的文章,才发现当年的认识实在太浅显,这里我把WCF的几个重要知识点总结以下. ABC概念 WCF服务的构成如下图 如你所见,Host即为宿主, ...