一、题目说明

这个题目是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. java8date

    Java 8:新的时间和日期API 在Java 8之前,所有关于时间和日期的API都存在各种使用方面的缺陷,因此建议使用新的时间和日期API,分别从旧的时间和日期的API的缺点以及解决方法.Java ...

  2. 3、示例(在java中使用JSON)

    教程链接(json-smple1.1.1.jar文件) 链接:http://pan.baidu.com/s/1qXPbYHm 密码:v0f0 如何使用java编程语言编码和解码JSON 首先准备环境以 ...

  3. selenium Python实现附件上传

    对于web页面的上传功能一般有两类实现方式:一类是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器:另一个类是插件上传,一般基于flash/javascript或者 ...

  4. markdown列表

    Markdown 列表 Markdown 支持有序列表和无序列表. 无序列表使用星号(*).加号(+)或是减号(-)作为列表标记: * 第一项 * 第二项 * 第三项 + 第一项 + 第二项 + 第三 ...

  5. div 悬浮

    一个小需求,鼠标移动一个产品那, 显示这个产品的具体信息 代码如下: <a href="javascript:void(0);" onclick="frameSea ...

  6. PL2303HXA自2012已停产

    解决"PL2303HXA自2012已停产,请联系供货商" USB不识别的问题: 问题是WIN10自带的驱动不兼容引起的,只能安装早期驱动,再驱动程序中选择旧版本驱动即可

  7. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

  8. agc026F Lotus Leaves

    题目链接 题目大意 一个n*m的网格上有一些点,一个点可以跳到与其同一行或同一列的点上.给定起点和终点. 求要使起点不能跳到终点,最少撤走几个点. \(n,m\leq 100\) 解题思路 考虑将能够 ...

  9. Java AQS 的胡言乱语修正版

    前言 适合读者:3 年以上经验的同学 谈到并发编程,基本上都会想到JDK 的 JUC 工具包,它包含 锁,并发工具类,原子类,线程池,还有阻塞队列,这是从网上找的一个大致的知识体系. 相信这些工具读者 ...

  10. Wordpress-微信机器人高级版

    微信机器人高级版是我爱水煮鱼开发的一款插件,功能很棒,运行此插件需要同时开启WPJAM Basic插件. 高级版5.0 版本对服务器要求非常高,只支持 Linux 服务器,PHP 要求 7.2 及以上 ...