【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 ...
随机推荐
- my.cnf 配置文件参数解释
my.cnf 配置文件参数解释: #*** client options 相关选项 ***# #以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如果你 ...
- strdup实现
char * strdup(char *str) { char * strNew; assert(str != NULL); strNew = (); strcpy(strNew,str); retu ...
- 使用Scala
1. 净资产应用实例 我们要构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件. ...
- 简单JavaScript语句实现搜索关键字高亮功能
高亮功能主要是指对页面中指定区域的指定文字进行高亮显示,也就是背景着色.一般在搜索结果页面会经常用到这个功能. 下面就为大家提供一种解决方案,用javascript实现. 首先在<head> ...
- 浅谈jQuery easyui datagrid操作单元格样式
今天项目上遇到问题,就是表格风格统一的问题,由于用了2个不同的框架,所以如果要大修比较麻烦,考虑到修改表格样式工作量会少很多,所以考虑修改jQuery easyui datagrid数据网格的样式. ...
- sass与less
刚刚发现sass这个东西,前端真热闹,下面比较一下这两者的共同点与区别. 开头总结一下,方便记忆:sass依赖后端计算能力,less依赖客户端的计算能力. 很多开发者不选择LESS是因为LESS输出修 ...
- 数据需求统计常用awk命令
原文:http://www.5iops.com/html/2013/script_0418/267.html 1.将时间转换为时间戳 select unix_timestamp('2009-10-26 ...
- Jacoco覆盖率工具使用调研
JaCoCo Java Code Coverage Library Jacoco是一个开源的覆盖率工具.Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以 ...
- MySQL监控和预警
https://blog.csdn.net/zhaowenbo168/article/details/53219860 1.摘要 本人从事Java Web开发,在项目开发中会用到很多中间件,本文主要介 ...
- 一、NoSQL入门概述