我无法将节点添加到空列表中

时间:2022-01-16 21:08:53

I stumbled upon a weird problem with adding two linked lists into a third one in Java, the first linked list "myList1", the second linked list "myList2" and the third one "myList3".

我偶然发现了一个奇怪的问题,即将两个链表添加到Java中的第三个链表中,第一个链表“myList1”,第二个链表“myList2”,第三个链表“myList3”。

The combining method is supposed to to add the first LinkedList "myList1" then the second "myList2" into the third LinkedList "myList3", but I faced a problem with adding them to third list while it's empty, but if the third list has at least one element every thing goes smoothly.

组合方法应该将第一个LinkedList“myList1”然后第二个“myList2”添加到第三个LinkedList“myList3”中,但我遇到了将它们添加到第三个列表时出现问题,但它是空的,但如果第三个列表中有每件事情顺利进行至少一个元素。

The code:

Node current = myList1.head;            
while (current != null) {                                                                       
    Node newcurrent = myList3.head;
    int h1 = current.getData();                                  
    Node newNode = new Node(h1);
    if (newcurrent == null)     
        //the problem is with this code                                             
        newcurrent = newNode;                           
    else {                                     
        if (newcurrent.getLink() == null) {
            newNode.setLink(newcurrent.getLink());                                         
            newcurrent.setLink(newNode);                                      
        } else {                                        
            Node current11 = newcurrent;                                                
            while (current11.getLink() != null) {
                current11 = current11.getLink();
            }                       
            current11.setLink(newNode);                          
        }                                    
    }                                                           
    current = current.getLink();                            
}

The node is not added to the third LinkedList if the third list is empty, and I tried many other codes but it didn't work either, but if I entered at least one element to the third LinkedList the list is added normally.

如果第三个列表为空,则节点不会添加到第三个LinkedList,并且我尝试了许多其他代码,但它也不起作用,但是如果我向第三个LinkedList输入了至少一个元素,则列表会正常添加。

other codes I tried :

我试过的其他代码:

newcurrent.setLink(newNode);

and

newNode = newcurrent; 
newcurrent = newNode;

and

newNode.setLink(newcurrent); 
newcurrent.setLink(newNode);

and

newNode.link = newcurrent; 
newcurrent.link = newNode;

2 个解决方案

#1


0  

You're overcomplicating this a bit, I think. The links within the list are already there. You only need to link myList3.tail to myList1.head, no need to loop through adding each node independently. Since you don't look like you are storing a tail, you'll need to iterate to the end of myList3 to find it.

我想,你有点过于复杂了。列表中的链接已经存在。您只需将myList3.tail链接到myList1.head,无需循环添加每个节点。由于您看起来不像存储尾部,因此您需要迭代到myList3的末尾才能找到它。

if (myList3.head == null)
    myList3.head = myList1.head;
else {
    Node list3iter = myList3.head;
    while (list3iter.getLink() != null) {
        list3iter = list3iter.getLink();
    }
    list3iter.setLink(myList1.head);
    }
}

One further note, I find it painful to try to keep track of names like current, current11, newcurrent, etc. They all mean just about the same thing to my brain. If you're like me, a bit more descriptive naming might help you keep track of what your variables are meant to be doing here.

还有一点需要注意,我发现尝试跟踪当前,当前11,新潮流等名称是很痛苦的。它们对我的大脑来说都意味着同样的事情。如果你像我一样,更具描述性的命名可能会帮助你跟踪你的变量在这里要做的事情。

#2


0  

Node newcurrent = myList3.head;
....
if (newcurrent == null)     
    //the problem is with this code                                             
    newcurrent = newNode;                           

Not sure why you have two lists, but the last line above is just assigning to local variable. Should it be as follows instead?

不确定为什么你有两个列表,但上面的最后一行只是分配给局部变量。它应该如下吗?

myList3.head = newNode

#1


0  

You're overcomplicating this a bit, I think. The links within the list are already there. You only need to link myList3.tail to myList1.head, no need to loop through adding each node independently. Since you don't look like you are storing a tail, you'll need to iterate to the end of myList3 to find it.

我想,你有点过于复杂了。列表中的链接已经存在。您只需将myList3.tail链接到myList1.head,无需循环添加每个节点。由于您看起来不像存储尾部,因此您需要迭代到myList3的末尾才能找到它。

if (myList3.head == null)
    myList3.head = myList1.head;
else {
    Node list3iter = myList3.head;
    while (list3iter.getLink() != null) {
        list3iter = list3iter.getLink();
    }
    list3iter.setLink(myList1.head);
    }
}

One further note, I find it painful to try to keep track of names like current, current11, newcurrent, etc. They all mean just about the same thing to my brain. If you're like me, a bit more descriptive naming might help you keep track of what your variables are meant to be doing here.

还有一点需要注意,我发现尝试跟踪当前,当前11,新潮流等名称是很痛苦的。它们对我的大脑来说都意味着同样的事情。如果你像我一样,更具描述性的命名可能会帮助你跟踪你的变量在这里要做的事情。

#2


0  

Node newcurrent = myList3.head;
....
if (newcurrent == null)     
    //the problem is with this code                                             
    newcurrent = newNode;                           

Not sure why you have two lists, but the last line above is just assigning to local variable. Should it be as follows instead?

不确定为什么你有两个列表,但上面的最后一行只是分配给局部变量。它应该如下吗?

myList3.head = newNode