【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 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.
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)
- /**
- * 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)
- {
- vector<ListNode*> v;
- ListNode* cur = head;
- v.push_back(cur);
- int size = ;
- while(cur != NULL)
- {
- cur = cur->next;
- //push_back the last NULL
- v.push_back(cur);
- size ++;
- }
- //delete index
- int ind = size - n;
- if(ind- < )
- //delete the head
- return head->next;
- else
- {
- v[ind-]->next = v[ind+];
- return head;
- }
- }
- };
仔细分析之后觉得浪费空间太多。
我们只是通过尾节点位置确定需要删除节点(n-1个偏移量),不需要其他的位置信息。
只需要两个位置相差n-1的指针,当前面的指针指向尾节点时,后面的节点即指向需要删除的节点。
由此产生解法二:
时间复杂度:O(n)
空间复杂度:O(1)
- /**
- * 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* tail = head;
- while(--n)
- tail = tail->next;
- ListNode* del = head;
- ListNode* predel = NULL;
- while(tail->next != NULL)
- {
- tail = tail->next;
- predel = del;
- del = del->next;
- }
- if(predel == NULL)
- //delete head
- return head->next;
- else
- {
- predel->next = del->next;
- return head;
- }
- }
- };
【LeetCode】19. Remove Nth Node From End of List (2 solutions)的更多相关文章
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【LeetCode】19. Remove Nth Node From End of List
题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...
- 【一天一道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 ...
- 【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 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 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 ...
- 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 ...
- 【leetcode❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- [Leetcode][Python]19: Remove Nth Node From End of List
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...
随机推荐
- python——获取数据类型:type()、isinstance()的使用方法:
python——获取数据类型 在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' ...
- mac下java的安装和升级以及相关环境设置
安装:brew cask install java8 如果存在多个java,可以设置JAVA_HOME指定java版本 打开终端,执行/usr/libexec/java_home -V 查看MAC下J ...
- maven依赖信息获取
1.mvn dependency:analyze 首先是"Used declared dependencies found",指项目中使用到,但是没有显示声明的依赖,如果有的话,需 ...
- [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]
1. 注冊Microsfot之后想提交bug, 发现有这个提示, 所以提交不了bug, 有能提交的提交下吧. You are not authorized to submit the feedback ...
- FLume监控文件夹,将数据发送给Kafka以及HDFS的配置文件详解
详细配置文件flume-conf.properties如下: ############################################ # producer config ###### ...
- Servlet监听器统计在线人数
监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...
- html调用servlet(JDBC在Servlet中的使用)(2)
5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...
- Objective-C:分类(Category、extension)
分类(Category .Extension) (一)分类的划分 (2) 1.(命名的类别)类别Category:只能添加新的方法,不能添加新变量. 2.(未命名的类别)类 ...
- JavaScript事件代理和事件委托
一.概述: 那什么叫事件委托呢?它还有一个名字叫事件代理,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件.那这是什么意思呢?网上的 ...
- React Native for Android 热部署图片自己定义方案
情景 热部署时,我们期望升级包中包括js代码与图片资源. bundle的热部署网上已经有两种方案了,一种是用反射,一种是利用RN自带函数.将bundle初始化时直接放到指定文件夹下,之后通过替换bun ...