简体   繁体   English

LinkedList-试图了解实现

[英]LinkedList - Trying to understand implementation

I am learning to solve complex algorithm. 我正在学习解决复杂的算法。 For that I came across the implementation for LinkedList. 为此,我遇到了LinkedList的实现。 I am trying to understand the above solution. 我试图了解上述解决方案。 In appendToTail I don't understand while loop and the line after the while loop. 在appendToTail中,我不了解while循环以及while循环之后的行。 In deleteNode I cannot see where the node is deleted. 在deleteNode中,我看不到该节点被删除的位置。

class Node {
    Node next = null;
    int data;

    public Node(int d) {
        data = d;
    }

    void appendToTail(int d) {
        Node end = new Node(d);
        Node n = this;
        while (n.next != null) {
            n = n.next;
        }
        n.next = end;
    }

    Node deleteNode(Node head, int d) {
        Node n = head;
        if (n.data == d) {
            return head.next; /* moved head */
        }
        while (n.next != null) {
            if (n.next.data == d) {
                n.next = n.next.next;
                return head; /* head didn’t change */
            }
            n = n.next;
        }
   }
}

Well here are two cases to consider: First, when the node is the first in the list. 好了,这里有两种情况需要考虑:首先,当节点在列表中的第一个时。 Then the head is just moved to the next node and the first node is no longer part of the list. 然后,磁头仅移动到下一个节点,而第一个节点不再是列表的一部分。

In the second case, we just iterate over the whole list node by node. 在第二种情况下,我们只是逐个节点遍历整个列表。 If we reach the node, whose next node needs deletion (which is checked by the if statement), it is changed to hold the node after the deleted node as next node (first line within the if statement). 如果我们到达需要删除其下一个节点的节点(通过if语句检查),则将其更改为将删除的节点之后的节点保留为下一个节点(if语句的第一行)。 This removes the node from the list. 这将从列表中删除该节点。 Here the head stays the same, as changing it would remove all elements before the node that is supposed to be deleted (if it was changed to the node after the deleted one). 此处的头部保持不变,因为更改头部将删除应该删除的节点之前的所有元素(如果已将其更改为已删除的节点之后的节点)。

When the node b is supposed to be deleted, all the node a has to do is to point to the node after b ( c ). 当应该删除节点b ,所有节点a要做的就是指向bc )之后的节点。 This is how the list would look: 列表如下所示:

... a -> b -> c -> ...  // before deletion
... a -> c -> ...       // after deletion, now a points to c

For a explanation with nicer visualization you can check here . 对于更好的可视化的解释,您可以在这里查看 The general case part is where the second case is described. 一般情况部分是描述第二种情况的地方。 The disposal of the removed isn't done explicitly in the implantation, as it is performed by the garbage collector. 移除操作在植入过程中并未明确进行,因为它是由垃圾收集器执行的。

LinkedLists have several implementations. LinkedLists有几种实现。 Some keep only a pointer to the head of the list. 有些只保留pointer列表headpointer Others keep track of the tail to accomplish appending to tail in O(1) 其他人跟踪尾巴以完成O(1)的尾巴附加

This implementation maintains no tail pointer, so you have to iterate through the list starting from the head 此实现不保留尾指针,因此您必须从头开始遍历列表

1 --> 2 --> null
^
head

So this refers to the head of the list. 因此, this是列表的头。 The while loop moves through the list until it reaches the end or (until the next node pointer in n equals null) while循环在列表中移动,直到到达末尾为止(或直到​​n中的下一个节点指针等于null)为止。

In the above example the loop would terminate here 在上面的示例中,循环将在此处终止

n  n.next
2   null

The loop would then exit and set: 然后,循环将退出并设置:

n.next = end

The new list looks like this: 新列表如下所示:

1 --> 2 --> 3
^
head

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

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