[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: 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.
题解: 删除链表中的倒数第N个元素,并返回修改后的链表。
要求: 只经过一次遍历完成上述操作。
经典面试题,找到一个链表的倒数第N个元素的衍伸。 在本题中需要额外的记录第N个元素的上一个元素,用于元素删除。
寻找链表的倒数第N个元素,设置两个指针,分别从链表的头部出发,一个先遍历N个元素, 然后两个指针同时向后遍历,当前一个指针到达链表的尾部时,后一个指针则到达第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 *pre,*first,*last;
ListNode ans();
pre=first=last=head;
ans.next=pre;
for(int i=;i<n;i++)
first=first->next; //find faster pointer while(first!=NULL)
{
first=first->next;
pre = last;
last=last->next;
}
pre->next = last->next;
if(last==head) return head->next;
else return ans.next;
}
};
转载请注明出处 http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Remove Nth Node From End of List的更多相关文章
- leetcode 题解 || 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 exam ...
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【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 ...
- 【leetcode】Remove Nth Node From End of List(easy)
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
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- 【JAVA、C++】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 ...
- Java [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 ...
- 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, Give ...
随机推荐
- stm32通信概述
本文提到的内容有以下几个方面: 通信概述 串口通信 I2C通信 CAN通信 SPI通信 I2S通信 USB通信 其他通信 一.通信概述 按照数据传送方式分: 串行通信(一条数据线.适合远距离传输.控制 ...
- Linux配置Oracle 11g自动启动
http://www.cnblogs.com/edwardcmh/archive/2012/05/11/2495671.html 安装完毕Oracle 11g每次都得手动启动 | 停止数据库(dbst ...
- bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装
bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装
- addClass()使用方法
含义:为元素添加指定的样式名 具体用法分为以下两种: 1.可以一次添加一个或者多个用空格隔开的样式名 例: <script> $("ul li").addClass(& ...
- Git(一):Eclipse中配置Git
在Eclipse中配置Git: 一,由于我使用的是新版本的eclipse j2ee 4.4默认集成git,所以不用手动安装git插件 二,Eclipse配置基础信息.Windows->Prefe ...
- Java 单例模式详解(转)
概念: java中单例模式是一种常见的设计模式,单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种. 单例模式有一下特点: 1.单例类只能有一个实例. 2.单例类必须自己自己创建自己的唯一实例. ...
- <转> win7下破解已安装的VS2008
http://renfan.blog.51cto.com/3466724/754301 在win7系统下,已安装的VS2008维护界面是不显示序列号输入的,需要按以下步骤进行才能破解 1.点击“卸载/ ...
- Elasticsearch-PHP 索引操作
索引操作 本节通过客户端来介绍一下索引API的各种操作.索引操作包含任何管理索引本身(例如,创建索引,删除索引,更改映射等等). 我们通过一些常见的操作的代码片段来介绍,然后在表格中列出剩下的方法.R ...
- MySQL内置功能之事务、函数和流程控制
主要内容: 一.事务 二.函数 三.流程控制 1️⃣ 事务 一.何谓事务? 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. # ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第十一周
java学习第十一周 本周,进行了java集合方面的知识,在博客园的帮助下,我的课前预习更有条理性,重点明确,本周的课堂反应明显更好了,首先,梳理一下本周知识点. Collection ...