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 ...
随机推荐
- vue全局配置----小白基础篇
今天学习vue全局配置.希望帮助我们去了解vue的全局配置,快速开发. Vue.config是vue的全局配置对象.包含Vue的所有全局属性: silent:boolean(默认值:false)--- ...
- Codeforces Round #432 (Div. 1) B. Arpa and a list of numbers
qtmd的复习pat,老子不想看了,还不如练几道cf 这题首先可以很容易想到讨论最后的共因子为素数 这个素数太多了,1-1e6之间的素数 复杂度爆炸 所以使用了前缀和,对于每个素数k的每个小区间 (k ...
- span是没有value标签的,要向获得标签内部的值改怎么办。
1,js实现 var div = document.getElementById('divId');var spans = div.getElementsByTagName('span');var s ...
- 指针数组与带参main函数
(一)指针数组 指针数组就是每一个元素存放一个地址,相当于一个指针变量.如:int *p[4]指针数组比较适合用来指向若干字符串,使得处理字符串更加灵活.例如,现在要将若干字符串按字母顺序由小到大输出 ...
- ThreadLocal原理
ThreadLocal类可以看作是当前线程的一个局部变量,只有当前线程可以访问,因此是线程安全的. ThreadLocal内部维护了一个ThreadLocalMap类,ThreadLocalMap是一 ...
- POJ 3167 Layout(差分约束)
题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- imageview无法显示图片:java.lang.RuntimeException: Canvas: trying to draw too large(281520000bytes) bitmap
图片太大需要压缩. 压缩方法:http://jingyan.baidu.com/article/cdddd41c3ef41153ca00e162.html 如果特别大(几十M),可以先用在线的图片压缩 ...
- Postman使用小技巧
Postman使用小技巧 2017-09-13 目录: 1 自动生成流水号2 保存响应结果 1 自动生成流水号 返回 为了让接口具有幂等性,在设计时,往往有一个字段是唯一的(比如流水号,交易编号等), ...
- 第三方工具 - echarts中 设置x||y轴文案、提示文字等为固定字数,超出显示"..."
起初看到这种需求的时候,我是这个状态 对,我是拒绝的,人家echats画出来就是一个canvas,你让我怎么加... 但是,作为一个"有点追求的"前端,我得想招试试总结下来,唯一的 ...
- 《Linux命令行与shell脚本编程大全》- 读书笔记3 - 理解shell
当用户登录终端的时候,通常会启动一个默认的交互式shell.系统究竟启动哪个shell,这取决于用户配置.一般这个shell都是/bin/shell.默认的系统shell(/bin/sh)用于系统sh ...