一、题目说明

这个题目是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. vue 中监听页面滚动

    监听页面滚动 在methods中定义一个方法 handleScroll() { //获取滚动时的高度 let scrollTop = window.pageYOffset || document.do ...

  2. jdk 9 10 11 12 13 新特性

    jdk 9 新特性 1.集合加强 jdk9 为所有集合(List/Set/Map)都增加了 of 和 copyOf 方法,用来创建不可变集合,即一旦创建就无法再执行添加.删除.替换.排序等操作,否则将 ...

  3. C++中map的介绍用法以及Gym题目:Two Sequences

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字(key),每个关键字只能在map中出现一次,第二个可能称为该关键字的值(value))的数据 处理能力,由于这个特性,它完成有可能 ...

  4. 总结String类的常用方法

    总结String类的常用方法 1. 获取字符串长度 public int length() 2. 获取字符串某一位置的字符 public char charAt(int index) 注意:字符串中第 ...

  5. HDU 4825 Xor Sum(字典树)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 这道题更明确的说是一道01字典树,如果ch[u][id^1]有值,那么就向下继续查找/ ...

  6. .click() 和 onclick方法

    onclick=""只能绑定一次,再次绑定会把之前的覆盖 $('').click()可以绑定多次,再次绑定会在前一个程序执行完后触发

  7. jmeter csv 插件讲解

    1.变量名称 name,pwd 格式表示因为文本中分割默认是逗号所以变量设置也是按此格式如果想按其他格式可以在分隔符栏自定义 2.忽略首行: 有的csv读取你希望读取的数据有header如: user ...

  8. oracle误操作表数据--回退(闪回)被提交后的数据

    // 查询该时间段 这个表的状态 (就是表状态正常的时刻 下面的时间仅用于举例) select * from 表名 as of timestamp to_timestamp('2019-09-26 1 ...

  9. 基于springboot通过注解AOP动态切换druid多数据源--mybatis

    控制于接口之上: 开始:demo地址  在lsr-core-base中 自定义注解: /** * @Description: 数据源切换注解 * @Package: lsr-microservice ...

  10. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...