[leetcode] 19. Remove Nth Node From End of List (Medium)
原题链接
删除单向链表的倒数第n个结点。
思路:
用两个索引一前一后,同时遍历,当后一个索引值为null时,此时前一个索引表示的节点即为要删除的节点。
Runtime: 13 ms, faster than 24.49% of Java
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode slow=head;
ListNode fast=head;
for(int i=0;i<n;i++)
fast=fast.next;
if (fast==null){
return head.next;
}
while(fast.next!=null)
{
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return head;
}
}
[leetcode] 19. Remove Nth Node From End of List (Medium)的更多相关文章
- [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 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 [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 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [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 ...
- [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
随机推荐
- 開發PlainTasks與JSON的插件
PlainTasks 是款很有名的任務管理插件,具體的介紹在這裡. 我最近的工作作務,是開發一款插件,能實現 JSON 文件到 todo 類文件的轉換. JSON 的格式是這樣的 1: { 2: &q ...
- 预处理器#include 指令
预处理器发现 #include 指令后,就会寻找后跟的文件名并把这个文件的内容包含到当前文件中.被包含文件中的文本将替换源代码文件中的#include指令,就像你把被包含文件中的全部内容键入到源文件中 ...
- linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- 海康威视频监控设备Web查看系统(二):服务器篇
声明:本系列文章只提供交流与学习使用.文章中所有涉及到海康威视设备的SDK均可在海康威视官方网站下载得到.文章中所有除官方SDK意外的代码均可随意使用,任何涉及到海康威视公司利益的非正常使用由使用者自 ...
- 在无界面centos7上部署jdk和tomcat
1.安装xshell6 2.创建服务器连接,输入用户名和密码 3.输入 sudo su -root 获取root权限 4.输入 cd /usr/local 进入local文件夹 5.输入 wget - ...
- ios开发系列之内存泄漏分析(上)
ios自从引入ARC机制后,一般的内存管理就可以不用我们码农来负责了,但是一些操作如果不注意,还是会引起内存泄漏. 本文主要介绍一下内存泄漏的原理.常规的检测方法以及出现的常用场景和修改方法. 1. ...
- Electron构建一个文件浏览器应用(一)
在window.mac.linux系统中,他们都有一个共同之处就是以文件夹的形式来组织文件的.并且都有各自的组织方式,以及都有如何查询和显示哪些文件给用户的方法.那么从现在开始我们来学习下如何使用El ...
- Spark学习之路(六)—— 累加器与广播变量
一.简介 在Spark中,提供了两种类型的共享变量:累加器(accumulator)与广播变量(broadcast variable): 累加器:用来对信息进行聚合,主要用于累计计数等场景: 广播变量 ...
- mysql group_concat的长度问题
mysql group_concat的长度问题 show variables like 'group_concat_max_len';+----------------------+-------+| ...
- 【设计模式】行为型03观察者模式(Observer Pattern)
记得16年初第一次学习了23种设计模式,但是除了少数几个简单的外,其他的很多都是学了个似懂非懂,以至于有人问起甚至说不上来,现在想想,其实就是没看懂而已.例如观察者模式,其实原理很简单,但是当时并没有 ...