LeetCode之“链表”: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: ->->->->, and n = . After removing the second node from the end, the linked list becomes ->->->.
Note:
Given n will always be valid.
Try to do this in one pass.
这道题比较常规的想法是先算出链表长度len,再将头指针移动len-n步就可以到达待删节点了。具体程序如下(4ms):
/**
* 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)
return head; int len = ;
ListNode *start = head;
while(start)
{
len++;
start = start->next;
} if(len == n)
{
start = head;
head = head->next;
delete start;
start = nullptr;
return head;
} ListNode *preNode = head;
start = preNode->next;
int k = ;
while(k < len - n)
{
preNode = start;
start = start->next;
k++;
} preNode->next = start->next;
delete start;
start = nullptr; return head;
}
};
另一个比较巧妙的方法就是先定义一个慢指针(初始值为头指针父指针),让头指针移动n步,再让头指针和一个慢指针同时移动直至头指针为空,这时慢指针指向的下一个节点就是待删节点。具体程序日如下(4ms):
/**
* 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)
return head; ListNode *dummy = new(nothrow) ListNode(INT_MIN);
assert(dummy);
dummy->next = head; for(int i = ; i < n; i++)
head = head->next; ListNode *preNode = dummy;
while(head)
{
head = head->next;
preNode = preNode->next;
} ListNode *delNode = preNode->next;
preNode->next = delNode->next;
delete delNode;
delNode = nullptr; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};
LeetCode之“链表”:Remove Nth Node From End of List的更多相关文章
- 《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 (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【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
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- 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题解(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】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: G ...
- LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【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 ...
- LeetCode:19. Remove Nth Node From End of List(Medium)
1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...
随机推荐
- android 调试工具ADB命令详解
adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用. 通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具. ...
- T-SQL动态查询(1)——简介
起因: 由于最近工作需要及过去一直的疑问,所以决定着手研究一下动态SQL.由于离开一线开发有点年头了,很多技巧性的东西没有过多研究,作为DBA和<SQL Server性能优化与管理的艺术> ...
- 在代码中写view 的长宽高等
获得资源的id的另一种方法 int layoutRes = getResources().getIdentifier("pager_view" + i, "layout& ...
- Anakia 转换xml文档为其他格式
一.简介 Anakia 使用JDOM 和Velocity将XML文档转换为特定格式的文档 二.解析xml文档方法 1.DOM java jdk,xml-api.jar 需要加载整个xml文档来构建层次 ...
- Spring Boot微服务架构入门
概述 还记得在10年毕业实习的时候,当时后台三大框架为主流的后台开发框架成软件行业的标杆,当时对于软件的认识也就是照猫画虎,对于为什么会有这么样的写法,以及这种框架的优势或劣势,是不清楚的,Sprin ...
- iOS下JS与原生OC互相调用(总结)
这是去年总结的一篇文章,也一并先放到这个目录下好了. iOS开发免不了要与UIWebView打交道,然后就要涉及到JS与原生OC交互,今天总结一下JS与原生OC交互的两种方式. JS调用原生OC篇 方 ...
- 详解EBS接口开发之销售订单导入
步骤 1. 创建一个订单导入来源. - 导航到 OM -> 设置 -> 订单 -> 导入来源 - 输入一个新的订单导入来源名称和描述 - 选择启用来激活 ...
- 由源代码编译SpriteBuilder最新版本1.5.0搭配最新的Cocos2D 3.4.9
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道SpriteBuilder版本停留在1.4.9已经很久 ...
- Tomcat 5.5 JNDI Resource 配置 (tomcat数据源配置)
转自:http://blog.csdn.net/fenglibing/article/details/4528512 Tomcat 5.5 JNDI Resource 配置 Author Blog:h ...
- 安卓仿QQ红包领取详情界面动画
为了能清楚的看到这个效果,本人不惜几次花费重金给众群叼发放红包,来查看红包领取详情界面的动画效果,QQ效果如图: 图中我们可以看到,动画处的头像和文字是一起的,即同时并且是整体,注意,是整体进行缩放的 ...