一、题目说明

这个题目是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的更多相关文章

  1. [刷题] 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不合 ...

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

  3. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

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

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

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

  6. (链表 双指针) 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 ...

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

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

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

随机推荐

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 M. Longest subsequence(思维+序列自动机)

    序列自动机跑s串 假设k为s和t相同的长度,初始时相同长度为0 取s串中大于t[i]的最左边的位置,用n-tmp+1+i-1更新答案,tmp是最左端的位置 然后去t[i]相等的位置,走到下一位,如果下 ...

  2. PS——牛奶字

    一.新建800*600像素的背景,设置前景色到透明渐变(黑到白),线性渐变,从上到下画一条直线 二.用矩形选框工具在背景上方1/2位置画一个矩形,Ctrl+Delete填充颜色 三.输入文字,设置图层 ...

  3. IIS-URL重写参数

    参考:https://www.cnblogs.com/gggzly/p/5960335.html URL 重写规则由以下部分组成: 模式 - 可以理解为规则,分通配符和正则匹配     条件 - 可以 ...

  4. Java面向对象编程 -6.4

    二维数组 数组的定义格式 数组的动态初始化:初始化之后数组中的每一个元素的保存的内容为其对应数据类型的默认值 声明并初始化数组: 数据类型 数组名称 [][] = new 数据类型[行个数][列个数] ...

  5. java 如何快速的获取浏览量

    最近公司做了一个类似 于发帖,交友圈一个这样的功能 在如何精确快速的获取用户的浏览量,且及时的更新显示,最初我是这样想,把每条帖子内容浏览量放到reids 里面,但是redis只是用来存零时数据,想想 ...

  6. HITCON-Training-master 部分 Writeup(1月30更新)

    0x01.lab3 首先checksec一下,发现连NX保护都没开,结合题目提示ret2sc,确定可以使用shellcode得到权限. IDA查看伪代码 大致分析: 将shellcode写入name数 ...

  7. 最常用的CountDownLatch, CyclicBarrier你知道多少? (Java工程师必会)

    CountdownLatch,CyclicBarrier是非常常用并发工具类,可以说是Java工程师必会技能了.不但在项目实战中经常涉及,而且在编写压测程序,多线程demo也是必不可少,所以掌握它们的 ...

  8. JDBC 通过读取文件进行初始化

  9. 无数据库模式kong/kong-ingress-controller

    apiVersion: v1kind: Namespacemetadata:  name: kong---apiVersion: apiextensions.k8s.io/v1beta1kind: C ...

  10. 配置SVTI

    路由器SVTI站点到站点VPN         在IOS 12.4之前建立安全的站点间隧道只能采用GRE over IPSec,从IOS 12.4之后设计了一种全新的隧道技术,即VIT(Virtual ...