【问题标题】:Can't visualize the insertion of a node at the end of a linked list无法可视化在链表末尾插入节点
【发布时间】:2019-10-26 17:28:09
【问题描述】:

我目前正在尝试理解和可视化 Java 中的链接列表。

我了解链表的基本概念以及如何在链表头部添加节点。但是,我不明白新节点是如何添加到链表末尾的。

例如,在以下代码中:

public class LinkedList
{
    Node head; // head of list

    /* Linked list Node*/
    class Node
    {
        int data;
        Node next;
        Node(int d) {data = d; next = null; }
    }

    public void append(int new_data)
    {
        /* 1. Allocate the Node &
        2. Put in the data
        3. Set next as null */
        Node new_node = new Node(new_data);

        /* 4. If the Linked List is empty, then make the
            new node as head */
        if (head == null)
        {
            head = new Node(new_data);
            return;
        }

        /* 5. This new node is going to be the last node, so
            make next of it as null */
        new_node.next = null;

        /* 6. Else traverse till the last node */
        Node last = head;
        while (last.next != null)
            last = last.next;

        /* 7. Change the next of last node */
        last.next = new_node;
        return;
    }

    public static void main(String[] args)
    {
        /* Start with the empty list */
        LinkedList llist = new LinkedList();

        // Insert 6. So linked list becomes 6->NUllist
        llist.append(6);

        // Insert 4 at the end. So linked list becomes
        // 6->4->NUllist
        llist.append(4);
        llist.printList();
    }

    public void printList()
    {
        Node tnode = head;
        while (tnode != null)
        {
            System.out.print(tnode.data+" ");
            tnode = tnode.next;
        }
    }
}

虽然我可以可视化遍历(last 到达 llist 的末尾),但我不明白为什么 last = last.next;(在 public void append(int new_data) 中)将节点链接到前一个节点(为什么前一个 @987654327 @ 指向它)。

感谢您的支持!

【问题讨论】:

标签: java linked-list


【解决方案1】:

基本链表只是从头节点开始,链表中的每个节点都指向链表中的下一个节点。最后一个节点将其next 设置为null,直到添加一个新节点,此时新节点成为最后一个节点。

所以逻辑在这里完成:

/* 6. Else traverse till the last node */
        Node last = head;
        while (last.next != null)
            last = last.next;

        /* 7. Change the next of last node */
        last.next = new_node;

它从头部开始,查看它的next。如果它不是null,则意味着它仍然不是列表的实际最后一个节点,因此它将last 变量设置为下一个。直到最后它找到将next 设置为null 的那个。 last 变量只有在 while 循环结束时才是真正的最后一个变量。

然后它只是将last 节点的next 设置为新节点。新节点已经将其next 设置为null,因此下次遍历完成时,它将是last 节点。

【讨论】:

  • 我知道last 应该放在列表的末尾,我也知道last.next 如何以及为什么指向new_node。我不明白的是前一个节点如何以及为什么有一个指向last 的指针。在当前代码中,为什么last 链接到另一个元素?它不应该是带有 6 值的节点的副本(因此不链接到先前的节点)吗?在下面的可视化中,我不明白第 34 > 35 步:shorturl.at/amIO5
  • last 没有放在任何地方。它只是对已经存在的节点的引用。这就像您的手指从头开始跟踪列表中的节点链(它显示Node last = head),然后通过检查指向下一个节点的next 引用进行跟踪,因此将您的手指移动到下一个节点列表中的项目。这就是while循环中发生的事情。当没有其他内容时(while 循环中的条件),您知道last 指向列表的真正结束节点。所以 new_node 会追随它。您可以通过设置 last.next 引用来做到这一点。
【解决方案2】:

如果您只是将代码更改为类似的代码,它仍然可以执行相同的操作,但会为您提供另一种观点,这可能会有所帮助。有时变量的命名会让你感到困惑。

为了更好地理解将last重命名为current,因为您从头部开始,然后依次查看每个节点,直到找到next指向null的节点,这表示列表的结尾。

请记住,.next 本身是一个节点,current 用作迭代器。 你也可以写成for-loop:

Node current = head;
for(current; current.next != null; current.next)
//now current refers to the last node since current.next now points to null
current.next = new_node;
//The new end is now new_node - new_node.next points to null

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 2011-08-13
    • 2020-11-15
    • 2021-05-23
    • 2015-02-08
    • 1970-01-01
    相关资源
    最近更新 更多