[刷题] 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, ...
随机推荐
- vue 快速入门 系列 —— 初步认识 vue
其他章节请看: vue 快速入门 系列 初步认识 vue vue 是什么 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. 所谓渐进式,就是你可以一步一步.有阶段 ...
- istio: 无法提供内部访问外部服务
现象 能够内部无法访问外部服务. 在部署测试服务 kubectl apply -f samples/sleep/sleep.yaml 设置环境变量 export SOURCE_POD=$(kubect ...
- 网络编程Netty入门:Netty的启动过程分析
目录 Netty的启动过程 Bootstrap 服务端的启动 客户端的启动 TCP粘包.拆包 图示 简单的例子 Netty编解码框架 Netty解码器 ByteToMessageDecoder实现类 ...
- 介绍一款能取代 Scrapy 的 Python 爬虫框架 - feapder
1. 前言 大家好,我是安果! 众所周知,Python 最流行的爬虫框架是 Scrapy,它主要用于爬取网站结构性数据 今天推荐一款更加简单.轻量级,且功能强大的爬虫框架:feapder 项目地址: ...
- JDK8新特性(二) 流式编程Stream
流式编程是1.8中的新特性,基于常用的四种函数式接口以及Lambda表达式对集合类数据进行类似流水线一般的操作 流式编程分为大概三个步骤:获取流 → 操作流 → 返回操作结果 流的获取方式 这里先了解 ...
- 035- 控制语句_break和continue
break break是java语言中的关键字,中文是打断,终止的意思 可以用在switch语句中,结束分支语句,防止case穿透现象的发生. 可以出现在循环当中,作用是结束整个循环的执行,默认情况下 ...
- 【ElasticSearch】ES线上脏数据处理
ES分组 GET index_user_latest/_search { "aggs": { "group_by_tags": { "terms&qu ...
- LA3213加密
题意: 白书上有些题的题意说的太蛋疼了,这个题的意思是说有两种加密方式,一种是交换位置,另一种是一一映射,交换位置是指如ABCD 可以加密成DCBA 也可以加密成ACBD就是把某些字母的位 ...
- hdu5015 矩阵快速幂233(好题)
题意: 给你一个(n+1)*(m+1)的矩阵mat,然后给你mat[0][1] = 233 ,mat[0][2] = 2333,mat[0][3] = 23333...,然后输入mat[1 ...
- python中实现打印特定字符变换
首先需要将 lib文件 放在该文件同一目录 使用的时候,先导入 from lib.common import print_msg ,然后调用里面的 print_msg() 方法即可! lib文件地址: ...