1. Remove Nth Node From End of List My Submissions QuestionEditorial Solution

    Total Accepted: 106592 Total Submissions: 361392 Difficulty: Easy

    Given a linked list, remove the nth node from the end of list and return its head.

For example,

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Submission Details

207 / 207 test cases passed.

Status: Accepted

Runtime: 8 ms

思路:利用快慢指针,快指针先走n步,然后快指针到末尾,那么慢指针走到第n个位置的前一个位置

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre=head,*p=head;
for(int i=0;i<n;++i){ //先向前走n步
if(p!=NULL)p=p->next;
else return head;
}
if(p==NULL)return head->next; //第n+1个位置为空,那么倒数第n个位置为首元素
while(p->next){
pre = pre->next; //快慢指针同时走,直到快指针到达最后一个节点
p = p->next;
}
pre->next = pre->next->next;//删除该节点
return head;
}
};

42-Remove Nth Node From End of List的更多相关文章

  1. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  2. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  3. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

  4. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

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

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

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

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

  8. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  9. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  10. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

随机推荐

  1. js 原型链详解

    目录 构造函数和实例 属性Prototype 属性__proto__ 访问原型上的方法 构造函数也有__proto__ 构造函数的原型也有__proto__ Object.prototype这个原型对 ...

  2. Netty:Netty的介绍以及它的核心组件(二)—— ChannelFuture与回调

    Callback 回调 一个 Callback(回调)就是一个方法,一个提供给另一个的方法的引用. 这让另一个方法可以在适当的时候回过头来调用这个 callback 方法.Callback 在很多编程 ...

  3. 字符串与模式匹配算法(二):MP算法

    一.MP算法介绍 MP 算法(Morris-Pratt算法)是一种快速串匹配算法,它是詹姆斯·莫里斯(James Morris)和沃恩·普莱特(Vaughan Pratt)在1970年提出的一种快速匹 ...

  4. 在纯JaveScript中实现报表导出:从“PDF”到“JPG”

    我们在前端报表中完成了各种工作数据的输入或内容处理之后,需要做什么? 数据的导出! 这些数据的常用导出格式有:PDF.Excel.HTML和图片几大类型. 但总有一些实际应用场景,需要的不仅仅是将现有 ...

  5. Go语言核心36讲(Go语言进阶技术十四)--学习笔记

    20 | 错误处理 (下) 在上一篇文章中,我们主要讨论的是从使用者的角度看"怎样处理好错误值".那么,接下来我们需要关注的,就是站在建造者的角度,去关心"怎样才能给予使 ...

  6. java核心技术 第3章 java基本程序设计结构

    类名规范:以大写字母开头的名词 若由多个单词组成 每个单词的第一个字母应大写(驼峰命名法)  与.java文件名相同 运行程序:java ClassName(dos命令) 打印语句:System.ou ...

  7. 设计模式学习-使用go实现原型模式

    原型模式 定义 代码实现 优点 缺点 适用场景 参考 原型模式 定义 如果对象的创建成本比较大,而同一个类的不同对象之间差别不大(大部分字段都相同),在这种情况下,我们可以利用对已有对象(原型)进行复 ...

  8. 【Python+postman接口自动化测试】(2)什么是接口?

    接口的概念 接口又称API(Application Programming Interface,应用程序编程接口),是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的 ...

  9. Code Runner,率先支持刚发布的 Visual Studio 2022!

    Visual Studio 被不少网友成为"宇宙第一IDE".但是,我写✍ PHP.Java 和 C#,也都是用的 VS Code. 我所在的组,是 Visual Studio C ...

  10. mysql 存储ipv6

    自定义列 https://groups.google.com/g/sqlalchemy/c/lZw0GipVYFw https://docs.sqlalchemy.org/en/14/core/cus ...