刷题19. Remove Nth Node From End of List
一、题目说明
这个题目是19. Remove Nth Node From End of List,不言自明。删除链表倒数第n个元素。难度是Medium!
二、我的解答
链表很熟悉了,直接写代码。
性能如下:
Runtime: 8 ms, faster than 35.76% of C++ online submissions for Remove Nth Node From End of List.
Memory Usage: 8.8 MB, less than 5.26% of C++ online submissions for Remove Nth Node From End of List.
#include<iostream>
using namespace std;
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;
if(n<0) return NULL;
int cur = n;
ListNode*p = head;
ListNode* nTh = p;
while(cur>0 && nTh!=NULL){
nTh = nTh->next;
cur--;
}
//n超过链表长度
if(nTh==NULL && cur>0) return head;
//删除第1个元素
if(nTh==NULL && cur==0){
ListNode * t = p->next;
if(t!=NULL){
head = p->next;
delete p;
return head;
}else{
delete p;
return NULL;
}
}
while(p!=NULL && nTh!=NULL && nTh->next!=NULL){
p=p->next;
nTh = nTh->next;
}
if(p!=NULL){
ListNode * tmp = p->next;
if(p->next !=NULL){
p->next = tmp->next;
}
delete tmp;
}
return head;
}
};
int main(){
Solution s;
ListNode dummy(0);
ListNode *p;
int i = 5;
while(i>0){
ListNode *tmp = new ListNode(i);
tmp->next = dummy.next;
dummy.next = tmp;
i--;
}
p = dummy.next;
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
ListNode*r = s.removeNthFromEnd(dummy.next,2);
p = r;
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
return 0;
}
三、改进
删除一个变量,性能大幅提高:
Runtime: 4 ms, faster than 88.76% of C++ online submissions for Remove Nth Node From End of List.
Memory Usage: 8.8 MB, less than 5.26% of C++ online submissions for Remove Nth Node From End of List.
改进后代码如下:
#include<iostream>
using namespace std;
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;
if(n<0) return NULL;
int cur = n;
ListNode*p = head;
ListNode* nTh = p;
while(cur>0 && nTh!=NULL){
nTh = nTh->next;
cur--;
}
//n超过链表长度
if(nTh==NULL && cur>0) return head;
//删除第1个元素
if(nTh==NULL && cur==0){
if(p->next!=NULL){
head = p->next;
delete p;
return head;
}else{
delete p;
return NULL;
}
}
while(p!=NULL && nTh!=NULL && nTh->next!=NULL){
p=p->next;
nTh = nTh->next;
}
if(p!=NULL){
ListNode * tmp = p->next;
if(p->next !=NULL){
p->next = tmp->next;
}
delete tmp;
}
return head;
}
};
int main(){
Solution s;
ListNode dummy(0);
ListNode *p;
int i = 5;
while(i>0){
ListNode *tmp = new ListNode(i);
tmp->next = dummy.next;
dummy.next = tmp;
i--;
}
p = dummy.next;
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
ListNode*r = s.removeNthFromEnd(dummy.next,2);
p = r;
while(p!=NULL){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
return 0;
}
再次改进:
class Solution{
public:
ListNode * removeNthFromEnd(ListNode* head,int n){
if(head==NULL) return NULL;
if(n<0) return NULL;
int len = 0;
ListNode*p = head;
while(p!=NULL){
p = p->next;
len++;
}
//n超过链表长度
if(len<n) return head;
//删除第1个元素
if(len==n){
head = head->next;
return head;
}
int t = len -n -1;
p=head;
while(t-->0){
p=p->next;
}
p->next = p->next->next;
return head;
}
};
刷题19. Remove Nth Node From End of List的更多相关文章
- [刷题] 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不合 ...
- 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, ...
随机推荐
- 【代码学习】PYTHON 生成器
一.生成器 一遍循环一遍计算的机制,称为生成器 二.生成器的特点: 1.节约内存 2.迭代到下一次的调用时,所使用的参数都是第一次所保留下的,即是说,在整个所有函数调用的参数都是第一次所调用时保留的, ...
- Spring IoC(三)bean属性、方法注释
1.环境配置 使用注解开发jdk1.5.Spring2.5支持,在xml中添加context相关的是四个配置; <beans default-lazy-init="true" ...
- PS——牛奶字
一.新建800*600像素的背景,设置前景色到透明渐变(黑到白),线性渐变,从上到下画一条直线 二.用矩形选框工具在背景上方1/2位置画一个矩形,Ctrl+Delete填充颜色 三.输入文字,设置图层 ...
- How to Start Learning Computer Graphics
Background Input\Output Image Knowledge Image Digital Image Processing Computer Vision Knowledge Com ...
- 获取SqlServer存储过程定义的三种方法
declare @p_text varchar(max) SELECT @p_text= text FROM syscomments WHERE id = ( SELECT id FROM sysob ...
- IDEA中使用Springboot+SSM的踩坑记(一)
今天由于电脑无限蓝屏,不知怎么把我IDEA里面破解过的一些东西给搞没了,包括IDEA本体和JRebel,照着原来的方法破解连本体都开不起来了(哭死),索性下了个最新版来用,结果JRebel还是破解不得 ...
- 解决小程序报错 Page "pages/index/main" has not been registered yet.
在小程序开发中,会频繁遇到 Page "pages/index/main" has not been registered yet. 这种报错,意思就说指定的页面没有注册,找 ...
- NAT-T和PAT(IPSec)
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥NAT-T技术介绍¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ 为什么TCP和UDP不能穿越:TCP和UDP有一个IP头的尾部校验(校验头部和负载 ...
- 判断一个数组是否包含一个指定的值 includes-ES6
var array1 = [1, 2, 3]; console.log(array1.includes(2)); // trueconsole.log(array1.includes(2, 5)); ...
- Python3中reduce和lambda的用法
reduce() 函数将一个数据集合(iterable[, initializer])可以看出是包含了初始化数据的,且初始化数据位列第1位,即集合中的第1个元素)中的所有数据进行下列操作:先对集合中的 ...