leetcode-algorithms-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 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.

Note:

Given n will always be valid.

Follow up:

Could you do this in one pass?

解法

O(n)内删除倒序第n个结点要一个循环找到第n个结点,可用两个链表指针,第一个比第二个多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)
{
if (head == nullptr) return nullptr; ListNode d(-1);
d.next = head; ListNode *first = &d;
ListNode *second = &d;
for(int i = 0; i < n; ++i) first = first->next; while(first->next)
{
first = first->next;
second = second->next;
} ListNode *del = second->next;
second->next = second->next->next;
delete del; return d.next;
}
};

空间复杂度: O(n).

时间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-19 Remove Nth Node From End of List的更多相关文章

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

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

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

  3. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

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

  5. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  6. LeetCode OJ 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, Give ...

  7. [Leetcode][Python]19: Remove Nth Node From End of List

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...

  8. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

  9. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...

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

随机推荐

  1. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  2. Visual Studio 安装easyX且导入graphics库后,outtextxy提示未定义标示符

    1.点击 “项目” ,然后点击 “属性”. 2. 然后点击左侧 “配置与属性” 下的 “常规” ,在点击 “字符集” ,选择 “使用多字节字符集” 即可解决问题

  3. 5、lvs使用进阶(01)

    四层.七层负载均衡的区别  https://jaminzhang.github.io/lb/L4-L7-Load-Balancer-Difference/   netfilter/iptables简介 ...

  4. 【Ruby】【目录 & 引用 & 文件 】

    [[目录]] 当前文件在根目录下一个文件夹下 引用当前文件所在目录上一级目录下某.rb文件 方法一 require File.join(File.dirname(FILE),'..','test_on ...

  5. 【Python】一些练习代码用的图片

  6. Program type already present:okio.AsyncTimeout$Watchdog Message{kind=ERROR, text=Program type :okio

    在app中的build.gradle中加入如下代码, configurations { all*.exclude group: 'com.google.code.gson' all*.exclude ...

  7. vs 2015 编译cocos2d-x-3.9

    下载地址:链接: https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ 提取码: p1pb 或者去官网下载. 解压后,进入build文件夹,点击如下工程链接 ...

  8. js,jq获取父,兄弟,子节点整理

    js获取节点 父: parentNode 获取已知节点的父节点. 子: childNodes; 得到全部子节点 children 得到全部子节点 firstChild 获得第一个子节点 lastChi ...

  9. Python3 数据库连接

    PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2中使用mysqldb. 数据库连接 连接数据库前,请先确认一下事项: 已经创建数据库testdb. 在test ...

  10. django特殊的标签和过滤器

    国际化标签和过滤器 Django还提供了一些模板标签和过滤器,用以控制模板中国际化的每个方面.它们允许对翻译,格式化和时区转换进行粒度控制. 1. i18n 此标签允许在模板中指定可翻译文本.要启用它 ...