[刷题] 19 Remove Nth Node From End of List
要求
- 给定一个链表,删除倒数第n个节点
示例
- 1->2->3->4->5->NULL , n=2
- 1->2->3->5
边界
- n是从0还是从1计
- n不合法,负数或者大于链表长度如何处理
思路
- 遍历一遍计算链表长度,再遍历一遍删除倒数第n个节点
- 使用两个指针同时移动,找到待删除节点的前一个节点

实现
1 struct ListNode {
2 int val;
3 ListNode *next;
4 ListNode(int x) : val(x), next(NULL) {}
5 };
6
7 class Solution {
8 public:
9 ListNode* removeNthFromEnd(ListNode* head, int n) {
10
11 assert( n>=0 );
12 ListNode* dummyHead = new ListNode(0);
13 dummyHead->next = head;
14
15 ListNode* p = dummyHead;
16 ListNode* q = dummyHead;
17 for( int i = 0 ; i < n + 1 ; i ++ ){
18 assert( q );
19 q = q->next;
20 }
21
22 while( q != NULL){
23 p = p->next;
24 q = q->next;
25 }
26
27 ListNode* delNode = p->next;
28 p->next = delNode->next;
29 delete delNode;
30
31 ListNode* retNode = dummyHead->next;
32 delete dummyHead;
33
34 return retNode;
35 }
36 };
相关
- 61 Rotate List
- 143 Reorder List
- 234 Palindrome Linked List
[刷题] 19 Remove Nth Node From End of List的更多相关文章
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- LeetCode题解(19)--Remove Nth Node From End of List
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
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
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
随机推荐
- HTTP2和 HTTPS来不来了解一下?
本文力求简单讲清每个知识点,希望大家看完能有所收获 一.HTTP协议的今生来世 最近在看博客的时候,发现有的面试题已经考HTTP/2了,于是我就顺着去了解一下. 到现在为止,HTTP协议已经有三个版本 ...
- Go+gRPC-Gateway(V2) 微服务实战,小程序登录鉴权服务(四):客户端强类型约束,自动生成 API TS 类型定义
系列 云原生 API 网关,gRPC-Gateway V2 初探 Go + gRPC-Gateway(V2) 构建微服务实战系列,小程序登录鉴权服务:第一篇 Go + gRPC-Gateway(V2) ...
- upload-labs通关历程
使用靶场前,先配置php版本为5.2,和下列对应配置. php.ini magic_quotes_gpc Off php<5.3.4 httpd.conf AddType applicatio ...
- 万字长文,带你彻底理解EF Core5的运行机制,让你成为团队中的EF Core专家
在EF Core 5中,有很多方式可以窥察工作流程中发生的事情,并与该信息进行交互.这些功能点包括日志记录,拦截,事件处理程序和一些超酷的最新出现的调试功能.EF团队甚至从Entity Framewo ...
- kafka-简介-01
1.kafka是什么? Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特 ...
- 敏捷史话(十五):我发明了敏捷估算扑克牌 —— James Greening
雪鸟会议 雪鸟会议前夕,James Grenning 在 Object Mentor 与 Robert C. Martin 一同工作,彼时组织雪鸟会议的 Bob 大叔盛情邀请 James,告知他会议的 ...
- Throwing cards away I UVA - 10935
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the botto ...
- Mycat Web监控工具
简介 Mycat-web 是 Mycat 可视化运维的管理和监控平台,弥补了 Mycat 在监控上的空白.帮 Mycat 分担统计任务和配置管理任务.Mycat-web 引入了 ZooKeeper 作 ...
- Day01_09_数据类型
数据类型 数据类型分类 *基本数据类型 *引用数据类型 基本数据类型 --第一类 整数型 byte short int long --第二类 浮点型 float double --第三类 布尔型 bo ...
- 简单好用微服务套件Anno&Viper DashBoard全新版来啦
1.Anno简介? Anno是一个微服务框架引擎.入门简单.安全.稳定.高可用.全平台可监控.依赖第三方框架少.底层通讯RPC(Remote Procedure Call)采用稳定可靠 ...