[Leetcode][019] Remove Nth Node From End of List (Java)
题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
【标签】 Linked List; Two Pointers
【个人分析】
这个题目应该算是Linked List里面的基础题。说它基础不是因为它简单,而是因为它是基石。一些 Linked list中经典方法在这道题里面都有应用。
1. Two Pointers 在 Linked List中: 如果能预先链表知道长度,那题目就简单多了,删掉从头开始的 Len - N就完事了。但是,LinkedList是没有办法预先知道链表长度的,所以需要借助双指针来利用offset来解决。
1.1)设置两个指针,fast 和 slow。
1.2) 让fast先单独走 n 步(这样,fast 和 slow这件就有了固定的差距:n)。
1.3) 然后两个指针一起往前走,当 fast.next = null, 即 fast是链表中最后一个结点时,slow就指在倒数第 n + 1个结点处, 也就是我们想要删除的那个结点前面的那个结点。
1.4) 让slow.next = slow.next.next,这样就成功删除掉了原先的slow.next
2. dummy head的应用: dummy head 就是我们自己新建一个结点,然后让原先的head结点接到这个新建结点的后面。我觉得用dummy head用三个比较明显的好处。
一个是可以尽量减少对原先链表的改变,如果是用head = head.next这样的方法,head会移动,这样就改变了原先的情况,不是很好。
二个好处是方便返回,直接返回dummy.next就可以。
三个是不用对头结点做特除对待,如果删除的结点恰好是头结点,还要为这种情况单独写一些代码,增加了代码量。
总之,如果要求返回新的头结点的链表题目,新建一个dummy head多半是比较好的选择。
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null || n <= 0) {
return null;
}
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode fast = dummyHead;
ListNode slow = dummyHead;
// let fast point go n steps itself
for (int i = 0; i < n; i++) {
assert(fast != null);
fast = fast.next;
}
// fast and slow go together
// until fast reaches the end of list
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
// now slow should pointing to the node
// before which we want to remove
slow.next = slow.next.next;
return dummyHead.next;
}
}
[Leetcode][019] Remove Nth Node From End of List (Java)的更多相关文章
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- 【JAVA、C++】LeetCode 019 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】019. 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 nth node from the end of list and return its head. For example, Give ...
- 【leetcode】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】Remove Nth Node From End of List(easy)
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 ...
随机推荐
- Linux下部署Symfony2对app/cache和app/logs目录的权限设置
在linux下部署完Symfony2,可能在访问的时候会报app/logs或者app/cache目录没有写权限的错误.在linux下,如果我们在命令行登陆的用户和web应用服务器(apache.ngi ...
- 生产环境搭建MySQL复制的教程(转)
[导读] 网络上有很多关于MySQL复制搭建的步骤和范例,以及手册上有一章完整的篇幅,讲述MySQL复制的原理.搭建步骤.优化等,但依然存在很多刚开始学习MySQL知识或者刚进入DBA行业的朋友咨询, ...
- Swift—默认构造函数-备
结构体和类的实例在构造过程中会调用一种特殊的init方法,称为构造函数.构造函数没有返回值,可以重载.在多个构造函数重载的情况下,运行环境可以根据它的外部参数名或参数列表调用合适的构造函数.默认构造函 ...
- 『软件介绍』SQLServer2008 基本操作
0x 01 连接数据库 Win7下,先打开SQLServer管理工具(开始菜单/所有程序/Microsoft SQL Server 2008/SQL Server Management Studio) ...
- Excel表无法正常打开
Excel表无法正常打开 处理:从菜单栏中的文件中,找到任何一个Excel表正常打开之后,在工具->选项->常规-> 去掉“忽略其他应用程序”,确认,关闭表格再次打开即正常.
- Cracking the coding interview--Q2.1
原文: Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- left join 关联条件位置
select e.last_name, e.department_id, d.department_name from hr.employees e left outer join hr.depart ...
- xcode教程,MAC常用命令
Vmware中为Mac Os安装vmtools 开机后,在Finder中就可以找到Darwin.iso,进行安装. 附件:vmtools for macos下载地址 https://softwareu ...
- Spark standalone安装(最小化集群部署)
Spark standalone安装-最小化集群部署(Spark官方建议使用Standalone模式) 集群规划: 主机 IP ...
- android 初学: 提示No Launcher activity found!
提示No Launcher activity found! 三步检查: 1 必须有 <category android:name="android.intent.category.LA ...