[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 ...
随机推荐
- scrapy核心组件工作流程和post请求
一 . 五大核心组件的工作流程 引擎(Scrapy)用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返 ...
- 【Oracle】Oracle改变日志归档模式
一.确认工作模式: 1.查询V$DATABASE SQL>select log_mode from v$database; 归档日志:ARCHIVELOG 2.执 ...
- C#做的在线升级小程序
转自原文C#做的在线升级小程序 日前收到一个小任务,要做一个通用的在线升级程序.更新的内容包括一些dll或exe或.配置文件.升级的大致流程是这样的,从服务器获取一个更新的配置文件,经过核对后如有新的 ...
- leetcode720
public class Solution { public string LongestWord(string[] words) { var maxlist = new List<string ...
- 9.redis安全
转自:http://www.runoob.com/redis/redis-tutorial.html Redis 安全 我们可以通过 redis 的配置文件设置密码参数,这样客户端连接到 redis ...
- Delphi IOS class_addMethod
class_addMethod 学习FMX.Platform.iOS.pas文件的处理办法 d:\program files (x86)\embarcadero\studio\17.0\source\ ...
- Delphi 拖动
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 ...
- 在Ubuntu 16.04如何安装Java使用apt-get的
转自:https://www.howtoing.com/how-to-install-java-with-apt-get-on-ubuntu-16-04/ 的Java和JVM(Java的虚拟机)是广泛 ...
- vs code 配置spring boot开发环境
一.环境变量 jdk环境变量一键设置 管理員运行 - 一支小白 - 博客园https://www.cnblogs.com/startnow/p/7416533.html 二.安装插件 1.Java E ...
- java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...