[Leetcode19] Remove Nth Node From End of List
视频讲解 http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html
(1)定义两个指针
ListNode fast = head;
ListNode slow = head;
(2)将快指针向前移动N步
(3.1)判断此时快指针是否已经到达尽头,如果是,头节点就是要删除的节点,返回head.next。
(3.2)将快慢两个指针同时以相同的速度往前移动,当快指针走到尽头的时候,慢指针的下一个位置就是倒数第N个节点,将慢指针next指向next.next.
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head;
ListNode slow = head; for(int i=0;i<n;i++){
fast = fast.next;
} if(fast == null){
head = head.next;
return head;
} while(fast.next != null){
fast = fast.next;
slow = slow.next;
} slow.next = slow.next.next;
return head;
}
}
[Leetcode19] Remove Nth Node From End of List的更多相关文章
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
- leetcode-algorithms-19 Remove Nth Node From End of List
leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《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解题报告—— 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 + ...
随机推荐
- collectionView
// /* UICollectionView 类是iOS6 新引进的API,用于展示集合视图, 布局更加灵活,可实现多列布局,用法类似于UITableView类. - 更新视图: [collectio ...
- 磁盘里的B,MB,GB,TB储存单位是怎么换算大小的?
磁盘里的B,MB,GB,TB是怎么换算大小的? 1TB=1024GB1GB=1024MB1MB=1024KB1KB=1024Byte 注:Byte就是B也就是字节KB是千字节MB是兆GB是吉字节 即千 ...
- C#中使用代码动态改变配置文件信息
static void Main(string[] args) { XmlDocument xDoc = new XmlDocument(); xDoc.Load("../../App.co ...
- struts2权威指南学习笔记:struts2引入自定义库
问题: 在jsp页面中添加了s:property标签,然而在页面始终未展示 解决: 经过搜索学习,发现只要添加语句 1 <%@ taglib prefix="s" uri=& ...
- Java数据结构与排序算法——堆和堆排序
//================================================= // File Name : Heap_demo //--------------------- ...
- 监控web页面的性能指标。
监控一个web页面的性能也是非常重要的,h5提供了一个非常好的属性来监控: window.performance 它有两个成员: navigation (一个叫做performanceNavi ...
- C# Pointer types
https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
- Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.
When I added the file in VS I forgot to set Build Action = None from the file properties.
- ConfuserEx
今天给大家介绍一个开源.net混淆器——ConfuserEx http://yck1509.github.io/ConfuserEx/ 由于项目中要用到.net 混淆器,网上搜寻了很多款,比如Dotf ...
- Win8 删除桌面右键中的显卡选项
打开注册表 regedit.exe HKEY_CLASSES_ROOT Directory Background shellex ContextMenuHandlers 按照上边的路径找过去.. 删除 ...