题目在这里: 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)的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. 【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 ...

  4. [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 ...

  5. 【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 ...

  6. 【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 ...

  7. [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, ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 魔法方法:构造和析构 - 零基础入门学习Python041

    魔法方法:构造和析构 让编程改变世界 Change the world by program 构造和析构 什么是魔法方法呢?我们来系统总结下: - 魔法方法总是被双下划线包围,例如__init__ - ...

  2. cat查看proc下文件帮助

    cat boot_mode 查看cpu版本 cat cmdline cat cpuinfo 查看cup详细信息 cat devices cat diskstats cat dma-mappings c ...

  3. recovery编译汉化源码开源地址

    本Recovery基于xiaolu开源的不完全汉化版源码,进行完全汉化,并合并Philz的最新源码. 汉化耗费我将近一整天的精力,纯手打,可能有遗漏或翻译不准的地方,请到微博反馈 本Rec完全开源,便 ...

  4. C# 文件创建时间,修改时间

    System.IO.FileInfo fi = new System.IO.FileInfo(@"D:\site\EKECMS\skin\Grey\default#.html"); ...

  5. 指定Action、Category调用系统Activity

    1.Intent对象详解 Android的应用程序包含三种重要组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来 ...

  6. C# and android and socket

    利用TCP协议通过Socket编写的网络聊天工具1-客户端 利用TCP协议通过Socket编写的网络聊天工具2-通用类设计 利用TCP协议通过Socket编写的网络聊天工具3-服务器端设计

  7. 实用C51编程的高级技巧(C51编程)

    一.C51热启动代码的编制 void main() { char data *HotPoint=(char *)0x7f; if((*HotPoint==0xaa)&&(*(--Hot ...

  8. Delphi 调试Dll报错 通过GetLastError显示错误信息。

    LibHandle := LoadLibrary('c:\windows\system32\SpcClass.dll');  ShowMessage(SysErrorMessage(GetLastEr ...

  9. Android视频录制

    public class MainActivity extends Activity { private MediaRecorder videoRecorder=null; private Butto ...

  10. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...