leetcode第19题--Remove Nth Node From End of List
Problem:
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.
果然一次就通过了。先读到尾,知道长度后,再从头开始读到要去掉的前一位,然后把要去掉的next复制给要去掉的前一个的next就可以。
/**
* 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)
{
int len = ;
ListNode *tmp = head;
while(tmp -> next != NULL)
{
len++;
tmp = tmp -> next;
}
if (len == n)
return head -> next;
ListNode *p = head;
for(int i = ; i < len - n; i++)
{
p = p -> next;
}
p -> next = p -> next -> next;
return head;
}
};
但是要挑战只遍历一次就通过就不能这样了。如下给了解法
http://blog.csdn.net/havenoidea/article/details/12918561
由于链表是单向的,不能倒着搜索,因此设置两个指针,他们相隔n个节点的距离,同布移动。
当前面的指针到达链表结尾的时候,后面一个指针离结尾的距离正好是n,再删除要求的节点就ok了。
要注意的我觉得应该有两点:
第一如果链表不到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) {
ListNode *left,*right,*Head;
right=head;
for(int i=;i<n;++i)
{
if(!right)return head;
right=right->next;
}
Head=left=new ListNode(-);
Head->next=head;
while(right)
{
right=right->next;
left=left->next;
}
ListNode *t1=left->next,*t2;
if(t1)left->next=left->next->next;
delete(t1);
t2=Head->next;
delete(Head);
return t2;
}
};
// blog.csdn.net/havenoidea
leetcode第19题--Remove Nth Node From End of List的更多相关文章
- LeetCode(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, G ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- 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, Gi ...
- 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 ex ...
- 《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 ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 乘风破浪:LeetCode真题_019_Remove Nth Node From End of List
乘风破浪:LeetCode真题_019_Remove Nth Node From End of List 一.前言 这次总算到了链表的操作了,之后肯定会有排序算法,二叉树,排序树,图等等的操作,现在我 ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
随机推荐
- nodejs的安装和使用
一 下载 下载地址: https://nodejs.org/download/ 二 安装 1 win7系统直接双击,就能够执行了: 2 win8须要使用管理员权限执行,否则会报错Error 2502, ...
- 提高duilib的richedit控制的一些特征
转载请注明原始出处.谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 假设要使用透明异形窗口功能,首先要改进duilib库让他 ...
- 如何成功实施SDL提供的官方Android平台Demo
如何成功实施SDL提供的官方Android平台Demo 作者:雨水 日期:2014-4-30 编写说明:SDL的官方提供了一个Anroid的demo模板SDLActivity,无法直接执行,依照官方 ...
- pydev-python 链接mysql数据库(mac系统)
1.首先,实现了命令行可以运行mysql 非常清楚了,直接引用过来,多谢哈.引用:http://www.lihui.info/mac-pydev-mysqldb/ ...
- 二十9天 月出冲击黑鸟 —Spring的AOP_AspectJ @annotation
6月14日,阴转雨. "四面垂杨十里荷,向云何处最花多, 画楼南畔夕阳和.天气乍凉人寂寞, 光阴须得酒消磨,且来花里听笙歌." 面向切面的框架AspectJ邂逅Spring,不仅造 ...
- UVA315- Network(无向图割点)
题目链接 题意: 给出一张无向图,求割点的个数 思路:非常裸的题目.直接套用模版就可以. 代码: #include <iostream> #include <cstdio> # ...
- 怎样批量把excel中已显示的科学计数法取消
作者:iamlaosong 把一文本文档拷贝到EXCEL中时,当中一列数字所有变成科学计数法,这些数事实上是条码号,不需进行运算,怎样能够取消科学计算法,将数字显示成原来的样子呢?一般方法例如以下: ...
- [置顶] 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!
前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...
- openstack 网络简史
openstack 网络简史 研究openstack有2个月的时间,这段时间从网上获取N多宝贵资料,对我的学习有非常大帮助,在加上我自己的研究,最终对openstack整个网络体系有了个浅显的认识,写 ...
- 第7章 桥接模式(Bridge Pattern)
原文 第7章 桥接模式(Bridge Pattern) 定义: 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能 ...