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 ...
随机推荐
- HTML5可以省略结束标记的元素
HTML5可以省略结束标记的元素 1.dd 2.dt 3.li 4.p 5.optgroup 6.option 7.rt 8.rp 9.thread 10.tfoot 11.tr 12.td 13.t ...
- Linux下使用Nginx端口转发出现502错误的一种解决办法
今天圈里的一个朋友在配置完nfinx80端口转发到5000后,发现一个问题 问题描述: 正确配置了Nginx80端口转5000端口,在CentOS上把.Net core WebAPI站点上传到cent ...
- IE浏览器兼容的常见问题及解决方案
常见6个问题及解决方案 1 IE6/IE7对display:inline-block的支持还欠缺 就是我们做导航栏时通常会用到<ul><li>标签去写,在现在一些主流的浏览器中 ...
- java实现马踏棋盘问题
1.问题描述: 在国际象棋中,马走日,用户输入棋盘的起始位置从x:0-4,y:0-3输出从这一点开始,马走完整个棋盘的各个方案,并输出方案数 2.输入样式: 请输入棋盘马起始位置: 0 0 3.输出样 ...
- npm包管理器小节一下
淘宝npm镜像cnpm设置 npm install -g cnpm --registry=https://registry.npm.taobao.org 更新npm的版本 npm install np ...
- SVN的安装以及和eclipse的结合使用
SVN概述 l 通常软件开发由多人协作开发,如果对代码文件.配置文件.文档等没有进行版本控制,将会出现很多问题: l 备份多个版本,占用磁盘空间大 l 解决代码冲突困难 l 容易引发BUG l 难于追 ...
- enable_shared_from_this类的作用和实现
使用举例 实际中, 经常需要在一个被shared_ptr管理的对象的内部获取自己的shared_ptr. 比如: 通过this指针来构造一个shared_ptr, 如下: struct Bad { v ...
- jQuary学习の二の语法
jQuery 语法是通过选取 HTML 元素,并对选取的元素执行某些操作.基础语法: $(selector).action() 美元符号定义 jQuery 选择符(selector)"查询& ...
- MMORPG中的相机跟随算法
先上代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cam ...
- 数据库学习笔记 4 强大的SQL
其实这篇文章应该至少一个星期前就应该更新了,但是最近小猿我和喜欢了好多年的女神牵手成功,所以这些天有点飘. ---创建表结构 create table tablename ( id int, name ...