简体   繁体   English

从列表末尾删除第 n 个节点 [LeetCode]

[英]Remov n-th node from the end of list [LeetCode]

The question is very straight forward and I got the algorithm as well.这个问题非常直接,我也得到了算法。 The only thing I don't understand is the return statement.我唯一不明白的是return语句。 why do we need to return dummy.next , when we haven't make any change to dummy at all.当我们根本没有对 dummy 进行任何更改时,为什么我们需要返回dummy.next

I guess my question is when we initially set slow and fast equal to dummy , aren't we just making a deep copy of the list/dummy (meaning whatever change we made in either of them does not affect dummy), or am i missing something..?我想我的问题是,当我们最初将slowfast设置为dummy时,我们不只是制作 list/dummy 的深层副本(这意味着我们在其中任何一个中所做的任何更改都不会影响 dummy ),还是我错过了某物..?

Any help would be appreciated.任何帮助,将不胜感激。 if you can back up your explanation with an example, would be awesome.如果你能用一个例子来支持你的解释,那就太棒了。

And here is link to the question:https://leetcode.com/problems/remove-nth-node-from-end-of-list/这里是问题的链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
         
        ListNode slow = dummy, fast = dummy;
        
        for(int i = 0; i <= n ; i++){
            fast = fast.next;
        }
        
        while(fast != null){
          fast = fast.next;
          slow = slow.next;
        }
        
        slow.next = slow.next.next;
        
        return dummy.next;
    }
}

The only difference that I can see between using the dummy and not would be in the case of having to remove the first element in the list.我可以看到使用 dummy 和不使用 dummy 的唯一区别是必须删除列表中的第一个元素。 In that case, without using the dummy there would not be a way to remove the head node.在这种情况下,如果不使用虚拟对象,就无法移除头节点。 However, this can easily be fixed by adding a simple check during the initial loop.但是,这可以通过在初始循环中添加一个简单的检查来轻松解决。

Here is my solution, omitting the use of the dummy head.这是我的解决方案,省略了虚拟头的使用。

public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode slow = head, fast = head;
        
    for(int i = 0; i <= n ; i++){
        if (fast == null) {
            return head.next;
        }
        fast = fast.next;
    }
        
    while(fast != null){
      fast = fast.next;
      slow = slow.next;
    }
        
    slow.next = slow.next.next;
        
    return head;
}

To answer your other questions,要回答您的其他问题,

aren't we just making a deep of the list/dummy我们不只是深入了解列表/虚拟

there is no deep copying going on since the dummy keeps a reference of the head, not a copy.没有进行深度复制,因为假人保留了头部的参考,而不是副本。

why do we need return dummy.next, when we haven't make any change to dummy at all.当我们根本没有对 dummy 进行任何更改时,为什么我们需要 return dummy.next。

the dummy is defined to be before the head.假人被定义在头部之前。 so when we want to return the changed head, we return the next node after dummy.所以当我们想要返回改变的头部时,我们在 dummy 之后返回下一个节点。 If we just returned dummy, then the answer that we would get would have an extra 0 in the front of it, which we don't want.如果我们只是返回 dummy,那么我们将得到的答案前面会有一个额外的0 ,这是我们不想要的。

To answer your questions,为了回答您的问题,

when we initially set slow and fast equal to dummy, aren't we just making a deep of the list/dummy当我们最初将慢速和快速设置为 dummy 时,我们不只是深入了解列表/dummy

No it doesn't.不,它没有。 The assignment is by reference as ListNode is a reference type.由于 ListNode 是引用类型,因此分配是通过引用进行的。 Thus changing any of dummy, slow, fast will show its affect on all three variables.因此,改变任何一个 dummy、slow、fast 都会显示出它对所有三个变量的影响。

the only thing I don't understand is the return statement.我唯一不明白的是return语句。 why do we need return dummy.next为什么我们需要 return dummy.next

As for the return statement, we still need to return the start of list that was provided to us as a parameter head in function, which is being pointed by dummy.next至于return语句,我们仍然需要返回在function中作为参数head提供给我们的list的开始,它被dummy.next所指向

As for why is the operations directly not performed on head and there is need of dummy variable and steps至于为什么不直接在head执行操作,需要虚拟变量和步骤

ListNode dummy = new ListNode(0);
dummy.next = head;

the reason might be cover up all edge scenarios.原因可能是掩盖了所有边缘场景。 eg: Linked List contains only one node and it is removed or the first node of linked list is removed. eg: Linked List 只包含一个节点并且被移除或者链表的第一个节点被移除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM