[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 + ...
随机推荐
- 符号(void *)何解?符号(void **)又何解??
http://bbs.csdn.net/topics/70050852 对于多级指针或者数组,要掌握正确的识别方法:void* 是说: 这是一个指针,去掉一个(*)就是它所指向的,在这里是指向放vo ...
- CentOS7搭建hadoop2.6.4双节点集群
环境: CentOS7+SunJDK1.8@VMware12. NameNode虚拟机节点主机名:master,IP规划:192.168.23.101,职责:Name node,Secondary n ...
- 通过Ajax方式上传文件,使用FormData进行Ajax请求
通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...
- Hadoop-2.7.3 问题收集
问题1:在DFS Lcation 上不能多文件进行操作: 在Hadoop上的每个节点上修改该文件 conf/mapred-site.xml 增加: <property> < ...
- JabRef 文献管理软件
JabRef 文献管理软件简明教程 大多只有使用LaTeX撰写科技论文的研究人员才能完全领略到JabRef的妙不可言,但随着对Word写作平台上BibTeX4Word插件的开发和便利应用,使用Word ...
- Robot Framework--11 RF结合Jenkins
转自:http://blog.csdn.net/tulituqi/article/details/17846463 为什么我们要引入RF?其实最初我们引入RF是为了能够快速的开展自动化验收测试,为敏捷 ...
- Django笔记-登陆、注册(利用cookie实现)
1.项目结构: 2.关键代码: settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'djang ...
- yii2权限控制rbac之rule详细讲解(转)
在我们之前yii2搭建后台以及rbac详细教程中,不知道你曾经疑惑过没有一个问题,rule表是做什么的,为什么在整个过程中我们都没有涉及到这张表? 相信我不说,部分人也都会去尝试,或百度或google ...
- C# MVC EF中匿名类使用
控制器中代码: var list = context.Says.Join( context.Users, a => a.UserId, b => b.Id, (a, b) => ne ...
- CSS样式表继承详解
最近在恶补css样式表的基础知识.上次研究了css样式表之冲突问题详解 .这次是对 css 继承 特性的学习. 什么是css 继承?要想了解css样式表的继承,我们先从文档树(HTML DOM)开始. ...