【问题标题】:I can't add a node into an empty list我无法将节点添加到空列表中
【发布时间】:2013-01-10 21:37:29
【问题描述】:

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

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

代码:

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();                            
}

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

我尝试过的其他代码:

newcurrent.setLink(newNode);

newNode = newcurrent; 
newcurrent = newNode;

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

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

【问题讨论】:

  • 从您的代码中删除空白行。
  • 如果你正确缩进你的代码,它会更容易跟踪、维护和调试。
  • 感谢编辑代码,很抱歉,这是我第一次使用这个网站
  • 为了减少一些复杂性,您可以考虑编写一种方法,将一个列表中的所有元素插入另一个列表。换句话说,如果需要,您一次只能处理两个列表,而不是三个。 IMO,这将使您的代码调试更简单。
  • @BasilBasaif private void InsertElements(LinkedList listSrc, LinkedList listDes){ //将元素从listSrc添加到listDes的代码 } 这就是你要找的吗?

标签: java linked-list nodes


【解决方案1】:

我认为你有点过于复杂了。列表中的链接已经存在。您只需要将myList3.tail链接到myList1.head,无需循环添加每个节点。由于您看起来不像在存储 tail,因此您需要迭代到 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);
    }
}

进一步说明,我发现尝试跟踪 currentcurrent11newcurrent 等名称很痛苦。它们对我的大脑意味着几乎相同的东西。如果您像我一样,更具描述性的命名可能会帮助您跟踪变量在此处的用途。

【讨论】:

  • 谢谢你,现在它可以在 myList3 为空时将其添加到列表中,但现在当 myList3 中有元素时它会进入 infinet 循环
  • 好的,两件事之一正在发生。可能,您创建了一个cyclic list,或者您误解了代码。我的答案中的代码替换了您问题中的所有代码,而不仅仅是 if/else 块。
  • 我替换了所有东西,但不是while循环,我是否也应该替换它,但是我需要遍历所有myList1,以便将其中的所有内容复制到myList3
  • 不,你没有。这就是我的观点。链表已经存在!如果我有两个链接列表:{n1->n2->n3}{nA->nB->nC},那么如果我创建从n3nA 的链接,那么列表是,没有任何其他链接或工作正在完成:{n1->n2->n3->nA->nB->nC}。您不需要单独添加它们(事实上,这样做是不正确的,除非您主动断开现有链接)。
  • THANK U V.MUCH,实际上它现在完美运行,感谢您的帮助,我非常感谢它
【解决方案2】:
Node newcurrent = myList3.head;
....
if (newcurrent == null)     
    //the problem is with this code                                             
    newcurrent = newNode;                           

不知道为什么你有两个列表,但上面的最后一行只是分配给局部变量。是不是应该改成下面这样?

myList3.head = newNode

【讨论】:

  • 谢谢你,但它也不起作用,问题是当第三个列表是空的但如果它里面至少有一个元素,第一个链表会被顺利添加。
  • myList1myList3 这两个列表是什么?
  • 我想将 myList1 添加到 myList3 中
猜你喜欢
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多