LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List
Total Accepted: 46720 Total Submissions: 168596My Submissions
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.
SOLUTION 1:
1.使用快慢指针,快指针先行移动N步。用慢指针指向要移除的Node的前一个Node.
2. 使用dummy node作为head的前缀节点,这样就算是删除head也能轻松handle啦!
主页君是不是很聪明呀? :)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//
ListNode dummy = new ListNode();
dummy.next = head; ListNode slow = dummy;
ListNode fast = dummy; // move fast N more than slow.
while (n > ) {
fast = fast.next;
// Bug 1: FORGET THE N--;
n--;
} while (fast.next != null) {
fast = fast.next;
slow = slow.next;
} // Slow is the pre node of the node which we want to delete.
slow.next = slow.next.next; return dummy.next;
}
}
GITHUB (国内用户可能无法连接):
LeetCode: Remove Nth Node From End of List 解题报告的更多相关文章
- [LeetCode] 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 @ Python
原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...
- 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, 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, 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, Give ...
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- LeetCode 237 Delete Node in a Linked List 解题报告
题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...
随机推荐
- App技术框架
一.App技术框架的类型 图1 三种App技术框架之间的关系 目前App的技术框架基本分为三种(图1): (1)Native App:互动型,iOS.Android.WP各一套,而且要维护历史版本,要 ...
- sscanf函数用法详解
sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed var1, mixed var2 ... ...
- BZOJ4293 [PA2015]Siano(线段树)
传送门 这Seg确实不好写,不过因为它与ai的相对顺序无关,所以,我们在对ai排序之后,就可做了.维护一个区间最大值,维护一个和,维护一个区间赋值的懒标记,再维护一个时间变化的标记就可以了. 因为不论 ...
- cordova3.X的部署和环境搭建教程
针对cordova3.0,至现在的3.6都能用. 一.准备工作: 1.下载Node.js 网址:http://nodejs.org/ 2.下载phonegap 打开CMD窗口 Windows平台: ...
- hdu 5726(二分)
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...
- HTTPD服务 openssl的https服务机制
环境: 环境: httpd服务器:10.140.165.169 CA服务器:10.140.165.93 CA服务器配置: 1.安装openssl [root@cnhzdhcp16593 ~]# yum ...
- Magicodes.WeiChat——缓存管理
本框架支持缓存管理,内部机制使用开源库CacheManager.支持全局缓存.租户缓存,默认使用的系统缓存实现,可以在Web.config将其配置为其他缓存类型,比如支持Redis.内存等. 开源库地 ...
- 从CLR角度来看值类型与引用类型
前言 本文中大部分示例代码来自于<CLR via C# Edition3>,并在此之上加以总结和简化,文中只是重点介绍几个比较有共性的问题,对一些细节不会做过深入的讲解. 前几天一直忙着翻 ...
- 让ASP.NET Web API支持text/plain内容协商
ASP.NET Web API的内容协商(Content Negotiation)机制的理想情况是这样的:客户端在请求头的Accept字段中指定什么样的MIME类型,Web API服务端就返回对应的M ...
- C# 中 async/await 调用传统 Begin/End 异步方法
最近在改进园子的图片上传程序,希望实现用户上传图片时同时将图片文件保存在三个地方:1)服务器本地硬盘:2)又拍云:3)阿里云OSS.并且在保存时使用异步操作. 对于异步保存到本地硬盘,只需用 Stea ...