[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 + ...
随机推荐
- Yocto开发笔记之《串口驱动调试》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 串口驱动各 ...
- sql自带函数语句
--取数值表达式的绝对值select abs(-41) 41select abs(41) 41select abs(-41.12) 41.12select abs(41.12 ...
- nginx 学习笔记(9) 配置HTTPS服务器--转载
HTTPS服务器优化SSL证书链合并HTTP/HTTPS主机基于名字的HTTPS主机带有多个主机名的SSL证书主机名指示兼容性 配置HTTPS主机,必须在server配置块中打开SSL协议,还需要指定 ...
- js初学—js全自定义单选框
代码如下: <script type="text/javascript"> window.onload=function() { var oIput=document. ...
- 处理字符串-String类和正则表达式
---基本元字符 . [] | () ---限定元字符 +至少匹配一个 *匹配0个或任意多个 ?匹配0个或1个(默认是贪心的) 当?在(*,+,?,{n},{ ...
- JSP 自定义标签
0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...
- CentOS6.x安装配置nginx
nginx安装 nginx的官网:http://nginx.org/ 相应下载页面:http://nginx.org/en/download.html 我这里使用nginx的yum在线安装 w ...
- 一个label里有个链接,点这个特别颜色的一些字,会执行一些操作
这个label在sb中或者纯代码创建的时候要是TTTAttributedLabel 然后进行相关设置
- Helixoft VSdocman 是一个集成于Visual Studio并提供了命令行版本的帮助文档编译工具
http://www.helixoft.com/vsdocman/overview.html https://blog.fishlee.net/2016/01/14/helixoft-vsdocman ...
- XML模块
XML 例子: # -*- encoding:utf-8 -*- import requests from xml.etree import ElementTree as ET f = request ...