【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 ...
随机推荐
- 最值得你所关注的10个C语言开源项目
. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接 ...
- C语言:结构体和联合体(共用体)
结构体:struct 1.结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员的整数倍. 3.结构体的总大小为结构体最宽基本类 ...
- 混沌数学之拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)
拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)是 1979年苏联物理学家拉比诺维奇和法布里康特提出模拟非平衡介 质自激波动的非线性常微分方程组: dot{x ...
- jQuery多文件下载
文件下载是一个Web中非常常用的功能,不过你是做内部管理系统还是做面向公众的互联网公司都会遇到这个问题,对于下载一般有点实际开发经验的都会自己解决,上周弄了一下多文件下载,业务场景就是一条数据详细信息 ...
- tomcat 用AXIS2发布WebService 网站的方法
Axis2+tomcat7.0 实现webService 服务端发布与客户端的调用. Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法. 第一步:首先要下载开发所需要的 ...
- C#中Serializable序列化
序列化就是是将对象转换为容易传输的格式的过程,一般情况下转化打流文件,放入内存或者IO文件 中.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象,或 ...
- Pylons架构网站开发从0到1
首先说明下这里的从0到1指的是从没有听说过pylons到开发出一个看上去还不错的网站.一个月前,我没有听说过也不知道什么是pylons,HTML只知道一些标签,JavaScript也不怎么懂,由于只倾 ...
- ajax局部刷新一个div下的jsp
用AJAX刷新一个DIV中的jsp内容 <script type="text/javascript"> var xmlhttp; function startrefre ...
- 思维导图软件xmind和mindmanager哪个更好
思维导图是一种将放射性思考具体化的方法,可以将人们的创造性思维及时捕捉并呈现,目前便捷的网络为人们带来了众多的思维导图软件,而在这些软件中只有亲身实践体验过,才能知道到底思维导图哪个好,哪个又适合自己 ...
- Sql server management studio: cannot find one or more components
Install VS2010 SHELL 独立组件 https://www.microsoft.com/en-US/download/details.aspx?id=1366 运行安装程序,rep ...