LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
package leetcode_50; /***
*
* @author pengfei_zheng
* 移除距离尾节点为n的节点
*/
public class Solution19 { public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head.next == null) {
return null;
}
int counter = 1;
ListNode start = new ListNode(0);
start.next = head;//保存头指针
ListNode fast = start;
ListNode slow = start;
while (fast.next != null) {//循环体
fast = fast.next;//快指针移动
if (counter <= n) {
counter++;//计数器
} else {
slow = slow.next;//慢指针移动
}
}
slow.next = slow.next.next;//移除
return start.next;
}
}
LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)的更多相关文章
- [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(删除链表中倒数第N个节点)
题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...
- [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 ...
随机推荐
- iOS: 动态更改 backBarButtonItem 的 title
先来看一下 UIBarItem 的 title 的描述 The title displayed on the item. You should set this property before add ...
- 正则grep
什么是正则: 正则就是一串有规律的字符串. 正则三剑客: grep/egrep (egrep是grep的扩展),sed, awk grep grep过滤指定关键词 [root@localhost gr ...
- ASP.NET程序也能像WinForm程序一样运行[转载]
阅读目录 开始 操作方式 支持的ASP.NET程序类别 它也是个HTTP服务器 支持远程机器访问 不受限于Windows防火墙 尊重每个人的操作习惯 内置多标签浏览器支持 启动参数及配置文件 支持 . ...
- 使用 UICollectionView 实现网格化视图效果
讲解 UICollectionView 的相关链接:http://blog.csdn.net/eqera/article/details/8134986 关键操作: 效果如下: KMCollectio ...
- 最新Java校招面试题及答案
本文作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向.在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几个部分: Java 基础知识点 Java 常见集合 ...
- 研究jenkins集成unittest成图
jenkins搭建完毕,unittest代码编写完毕,触发unittest执行测试的脚本和任务编写完毕,接下来研究生成的结果在页面的可视化. 方案: highcharts 参考资料: http://b ...
- @PropertySource加载文件的两种用法以及配置文件加载顺序
第一种: 现在我把资源文件的路径放在application.properties里 config.path=/home/myservice/config.properties @PropertySou ...
- 【scala】 scala 条件控制 和异常处理(二)
1.scala 变量定义 ,var val 区别. var 定义可变变量 val 定义不可变变量,scala 推荐使用.相当于Java的final 变量. scala中包含的基本数据类型详情如下表所示 ...
- [web] spring boot 整合MyBatis
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- linux系统调用函数---12
Linux应用编程学习笔记 周学伟 一.系统调用文件编程 1.文件打开函数 /*************************** ...