[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 ...
随机推荐
- HttpHelper工具类
/// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提 ...
- OC 代码规范
我们写出来的代码会给很多人看,为了使代码清晰简洁,方便阅读理解,都会统一遵从一定的代码规范,Objective-C同样如此. 主要参考规范: 1. Google Objective-C Sty ...
- 卓越网的kindle paperwhite
卓越网的kindle paperwhite, 899元的价钱,好吸引啊,我是不是也应该买一个呢,从卓越网中看见kindle paperwhite的1代开始,一直想买,等到现在的2代也出了,也继续在考虑 ...
- CP1W-CIF41以太网模块(使用总结)
1>插入CP1W-CIF41后,ERR灯一直闪烁? 解决方法:CIF41以太网模块在第一个扩展板槽位,需要把CP1H的4号拨码开关拨到ON的位置.同样,如果在第二个槽位,需要5号拨码拨到ON位置 ...
- 关于Layouts的分类
ViewGroup ---------------------------------LinearLayout.Relative Layout. AdapterView --------------- ...
- [LeetCode 115] - 不同子序列(Distinct Subsequences)
问题 给出字符串S和T,计算S中为T的不同的子序列的个数. 一个字符串的子序列是一个由该原始字符串通过删除一些字母(也可以不删)但是不改变剩下字母的相对顺序产生的一个新字符串.如,ACE是ABCDE的 ...
- 将Altium中的原理图与PCB导出为PDF的步骤与方法
1.通过File-Smart PDF(文件-智能PDF),快捷键F-M打开. 2.选择打印的范围:Current Project还是Current Document,还有生成的PDF的存放位置. 3. ...
- MFC的GUI窗口使用Console输出函数printf
在GUI程序中使用printf函数: #include <io.h> #include <fcntl.h> void InitConsole() { ; FILE* fp; A ...
- Java实现SOAP协议 之 HelloWorld
Java实现SOAP协议 之 HelloWorld SOAP是把基于HTTP的WEB技术与XML的灵活性和可扩展性组合在了一起. 准备: 1. 准备一个web server.Tomcat 下载地址 h ...
- 面试问题:关于java并发方面的
主要是回答一下面试中可能会碰到的问题.慢慢的积累一下.一个星期以后,补全.