Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有个小小困惑,这行代码是多余的吧 #538

Open
songyaolun opened this issue Mar 9, 2024 · 1 comment
Open

有个小小困惑,这行代码是多余的吧 #538

songyaolun opened this issue Mar 9, 2024 · 1 comment

Comments

@songyaolun
Copy link

newNode.next = q.next;

    //顺序插入
    //链表尾部插入
    public void insertTail(int value){

        Node newNode = new Node(value, null);
        //空链表,可以插入新节点作为head,也可以不操作
        if (head == null){
            head = newNode;

        }else{
            Node q = head;
            while(q.next != null){
                q = q.next;
            }
            newNode.next = q.next;
            q.next = newNode;
        }
    }

只有q.next等于null才会跳出循环,newNode本身创建时的next已经是null了,这里将null赋值给newNode的next好像有点冗余,而且让人困惑。

@yuexiaying
Copy link

是的,我也有这个疑惑,不需要这个呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants