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. es7集群的搭建

    es7集群的搭建 一.需求 二.前置条件 三.搭建步骤 1.下载es 2.创建 `es` 用户并给es目录授权 3.修改es的配置文件 4.es01目录下 elasticsearch.yml 的一个完 ...

  2. spring、spring boot中配置多数据源

    在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...

  3. 从0到1使用Kubernetes系列(五):Kubernetes Scheduling

    前述文章介绍了Kubernetes基本介绍,搭建Kubernetes集群所需要的工具,如何安装,如何搭建应用.本篇介绍怎么使用Kubernetes进行资源调度. Kubernetes作为一个容器编排调 ...

  4. AtCoder Beginner Contest 210题解

    A B 过水,略... C 统计长度为k的区间的最多本质不同的数.用尺取法维护下左右指针就可以了.调了许久的原因是更新答案时出现了问题. 当我移动指针时,我们应该移动一个就更新一个,而不是将移动与更新 ...

  5. 测试平台系列(71) Python定时任务方案

    大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 定时任务 定时任务,顾名思义: ...

  6. anaconda无法launch应用(无法l打开任何应用)的问题解决 (点击应用无反应)

    遇到了anaconda 无法launch 任何应用. 重装也不行. 先说我最终的解决方法(在官方文档中找到): 1. 启动 anaconda prompt , 输入 conda remove anac ...

  7. 2020美亚团体—Daniel篇

    Daniel的桌上计算机的哈希值(SHA-256)是甚么? 通过取证大师计算 SHA-256值为 07DD40CF28603F421F3A09CD38F1C8AA40A2AC4BFB46ECF8299 ...

  8. 『学了就忘』Linux基础命令 — 33、管道符

    目录 1.管道符介绍 2.管道符应用 (1)例子1: (2)例子2: (3)例子3: 1.管道符介绍 管道符|,也是Shell命令. 管道符的作用是链接多个命令,把命令1的结果作为命令2的操作对象. ...

  9. Spring Cloud调用接口过程

    Spring Cloud 在接口调用上,大致会经过如下几个组件配合: Feign== >Hystrix ==>Ribbon ==>Http Client(apache http co ...

  10. VMware软件虚拟机不能全屏的问题 & CentOS 安装Vmware Tools

    修改设置 1) 如下图右单击虚拟机名,选择[settings-],调出虚拟机设置界面. 2) 在设置界面选择[hardware]->[CD/DVD2(IDE)]->[Connection] ...