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,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
删除给定的倒数的第n个节点。我想的是数一遍这个ListNode的长度,然后减去n,再删除。上代码:
public static ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null)return head;
int length=0;
ListNode headCopy1=head;
ListNode headCopy2=head;
//calculate length
while(headCopy1!=null) {
length++;
headCopy1=headCopy1.next;
}
//remove first node,return head.next
if(n==length)return head.next;
for(int i=0;i<(length-n-1);i++) {
headCopy2=headCopy2.next;
}
if(headCopy2.next.next==null) {
headCopy2.next=null;
}else {
headCopy2.next=headCopy2.next.next;
}
return head;
}
后面看了别人的思路,采用的是快慢指针,先在head前面加一个节点start,这个很重要,如果只有一个节点,且要删除这个节点,虽然可以直接返回null,但是在统一性上来说,就多了几行单独出来的代码,快慢指针都指向这个start。快指针先走,走n步,然后再让快慢指针一起走,直到快指针为null,这时候慢指针后面的即为要删除的,通过快慢指针实现了计算length-n,很巧妙。然后让next跳过一个节点即可。再有就是最后的返回值,一开始我以为返回head和start.next都可以,结果我用head来返回的时候就报错了。因为head始终指的是头节点,即使头节点删除了,也还是会返回值,而start这个节点是与返回值无关的一个节点,对start不会有任何操作,如果只有一个节点即head节点,且要删除这个节点,你再返回head就不对了,而返回start.next则为空。
public static ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start=new ListNode(0);
start.next=head;
ListNode fast,slow;
fast=slow=start;
for(int i=0;i<n+1;i++) {
fast=fast.next;
}
while(fast!=null) {
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return start.next;
//return head;
}
Leetcode 19——Remove Nth Node From End of List的更多相关文章
- [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 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 ...
- 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 ...
随机推荐
- java并发:Synchronized 原理
1.同步代码块: 反编译结果: monitorenter : 每个对象有一个监视器锁(monitor).当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取moni ...
- I Hate It HDU - 1754
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- MySQL中information_schema数据库的内容
大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个information_schema数据库. information_schema数据库是做什么用的呢,使用WordPress博客 ...
- 使用 LINQPad 助力 LINQ 学习
简介一图示意 简介 LINQPad 是一款学习 LINQ,优化 SQL 的好助手. 它的一大特点是内置了新版<C# in a Nutshell>的全部 LINQ 示例,不管是配合原书进行练 ...
- table问题汇总
平时要使用 table 的次数说多不多,说少不少,但每次使用必定会被几个问题"坑"一下,所以这篇博客用来记录下table那些"小坑".但 table 也会有很多 ...
- lvs-dr 模式-piranha
系统: redhat 6.5 mini 机器名 ip vip lvs01(主lvs) 192.168.20.10 192.168.20.254 lvs02(备lvs) 192.168.20.20 rs ...
- Luogu 睡觉困难综合征 ([NOI2014]起床困难综合症)
一.[NOI2014]起床困难综合症 题目描述 网址:https://daniu.luogu.org/problemnew/show/2114 大意: 有一条链,链上每一个节点包含一个位运算f 与 一 ...
- [.NET]使用十年股价对比各种序列化技术
1. 前言 上一家公司有搞股票,当时很任性地直接从服务器读取一个股票10年份的股价(还有各种指标)在客户端的图表上显示,而且因为是桌面客户端,传输的数据也是简单粗暴地使用Soap序列化.获取报价的接口 ...
- 24时区,GMT,UTC,DST,CST时间详解
全球24个时区的划分 相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究竟 ...
- git pull error
在图形界面中,执行拉取操作时,出现下面的错误. You asked to pull from the remote 'origin', but did not specifya branch. Bec ...