https://leetcode.com/problems/remove-nth-node-from-end-of-list/

使用双指针法,可以仅遍历一次完成节点的定位

/**
* 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* l = new ListNode();
l = head;
ListNode* r = new ListNode();
r = l;
while(n--)
r = r->next;
//用于判断head是否为单节点链表
if(r == NULL)
return head->next;
while(r != NULL && r->next != NULL)
{
r = r->next;
l = l->next;
}
ListNode* temp = new ListNode();
temp = l->next;
l->next = l->next->next;
if(temp)
delete(temp);
return head;
}
};

19. Remove Nth Node From End of List C++删除链表的倒数第N个节点的更多相关文章

  1. 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点

    题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...

  2. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  3. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  4. 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)

    Level:   Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...

  5. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

    题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...

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

  7. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

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

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

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

随机推荐

  1. 免费api

    聚合数据提供30大类,100种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免费的各类API接口. 聚合的免费API接口数据: 手机号码归属地API接口:https://www. ...

  2. 小米MAX开发者选项 以及如何连接MAC开发RN

    打开开发者选项:设置--我的设备---全部参数-- 多次点击MiUI版本 打开开发者选项 然后返回到设置的主页面里面的更多设置就可以看到开发者选项了 在开发者选项中打开 USB调试/USB安装 将启动 ...

  3. RN 上传文件到以及上传文件到七牛云(初步)

    本文将介绍: 如何使用原生 Javascript 上传文件 如何使用七牛云 SDK 上传文件到七牛云 在 App 中文件上传是一个非常重要的需求,但是翻遍 React Naitve 的官方文档没有发现 ...

  4. 前端性能优化之按需加载(React-router+webpack)

    一.什么是按需加载 和异步加载script的目的一样(异步加载script的方法),按需加载/代码切割也可以解决首屏加载的速度. 什么时候需要按需加载 如果是大文件,使用按需加载就十分合适.比如一个近 ...

  5. _event_spawn

    EventId 事件ID Entry 刷新生物或物体的entry,正数为生物,负数为物体 Phase 刷新生物或物体的阶段ID SpawnTime 刷新生物或物体的时间(单位:秒),从阶段开始时开始计 ...

  6. 遍历一个可迭代对象中的所有元素,但是却不想使用for循环

    def manual_iter(): with open('/etc/passwd') as f: try: while True: line = next(f) print(line, end='' ...

  7. python (协程)生产者,消费者

    #coding=utf- import gevent from gevent.queue import Queue, Empty import time tasks = Queue(maxsize=) ...

  8. 1.1.1 vue-cli脚手架工具

    参考文档: windows下npm安装vue(以下教程大部分都是参考这篇博客的,按照着这篇博客自己实现了一遍) npm安装vue-cli脚手架 一.前言 npm:nodejs下的包管理器,安装好nod ...

  9. dll多个版本问题

    在配置文件设置不同版本的dll即可 配置文件如下 configuration 节点下面的  runtime 节点新增各个版本配置内容 <runtime> <assemblyBindi ...

  10. Qt5标准文件对话框类

    getOpenFileName()函数返回用户选择的文件名,其函数形式如下: QString QFileDialog::getOpenFileName(QWidget *parent = Q_NULL ...