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. Given linked list: 1->2->3->4->5, and n = 2.
  2.  
  3. After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

这题的难点在于one pass

没到尾部就没法进行倒退n个节点的操作。

但是到达尾部之后,再进行倒退删除操作,就不满足one pass了

由此诞生解法一:使用数组记录所有节点的位置,通过下标计算(size-n)立刻定位到需要删除的节点了

建立vector存放ListNode*,每个指针指向一个链表节点

时间复杂度:O(n)

空间复杂度:O(n)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution
  10. {
  11. public:
  12. ListNode *removeNthFromEnd(ListNode *head, int n)
  13. {
  14. vector<ListNode*> v;
  15. ListNode* cur = head;
  16. v.push_back(cur);
  17. int size = ;
  18. while(cur != NULL)
  19. {
  20. cur = cur->next;
  21. //push_back the last NULL
  22. v.push_back(cur);
  23. size ++;
  24. }
  25. //delete index
  26. int ind = size - n;
  27. if(ind- < )
  28. //delete the head
  29. return head->next;
  30. else
  31. {
  32. v[ind-]->next = v[ind+];
  33. return head;
  34. }
  35. }
  36. };

仔细分析之后觉得浪费空间太多。

我们只是通过尾节点位置确定需要删除节点(n-1个偏移量),不需要其他的位置信息。

只需要两个位置相差n-1的指针,当前面的指针指向尾节点时,后面的节点即指向需要删除的节点。

由此产生解法二:

时间复杂度:O(n)

空间复杂度:O(1)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *removeNthFromEnd(ListNode *head, int n) {
  12. ListNode* tail = head;
  13. while(--n)
  14. tail = tail->next;
  15.  
  16. ListNode* del = head;
  17. ListNode* predel = NULL;
  18. while(tail->next != NULL)
  19. {
  20. tail = tail->next;
  21. predel = del;
  22. del = del->next;
  23. }
  24. if(predel == NULL)
  25. //delete head
  26. return head->next;
  27. else
  28. {
  29. predel->next = del->next;
  30. return head;
  31. }
  32. }
  33. };

【LeetCode】19. Remove Nth Node From End of List (2 solutions)的更多相关文章

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

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

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

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

  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】019. 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 ...

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

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

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

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

  8. 【leetcode❤python】 19. Remove Nth Node From End of List

    #-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...

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

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

随机推荐

  1. python——获取数据类型:type()、isinstance()的使用方法:

    python——获取数据类型   在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' ...

  2. mac下java的安装和升级以及相关环境设置

    安装:brew cask install java8 如果存在多个java,可以设置JAVA_HOME指定java版本 打开终端,执行/usr/libexec/java_home -V 查看MAC下J ...

  3. maven依赖信息获取

    1.mvn dependency:analyze 首先是"Used declared dependencies found",指项目中使用到,但是没有显示声明的依赖,如果有的话,需 ...

  4. [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]

    1. 注冊Microsfot之后想提交bug, 发现有这个提示, 所以提交不了bug, 有能提交的提交下吧. You are not authorized to submit the feedback ...

  5. FLume监控文件夹,将数据发送给Kafka以及HDFS的配置文件详解

    详细配置文件flume-conf.properties如下: ############################################ # producer config ###### ...

  6. Servlet监听器统计在线人数

    监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...

  7. html调用servlet(JDBC在Servlet中的使用)(2)

    5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...

  8. Objective-C:分类(Category、extension)

    分类(Category .Extension) (一)分类的划分     (2) 1.(命名的类别)类别Category:只能添加新的方法,不能添加新变量.           2.(未命名的类别)类 ...

  9. JavaScript事件代理和事件委托

    一.概述: 那什么叫事件委托呢?它还有一个名字叫事件代理,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件.那这是什么意思呢?网上的 ...

  10. React Native for Android 热部署图片自己定义方案

    情景 热部署时,我们期望升级包中包括js代码与图片资源. bundle的热部署网上已经有两种方案了,一种是用反射,一种是利用RN自带函数.将bundle初始化时直接放到指定文件夹下,之后通过替换bun ...