简体   繁体   English

合并两个排序列表,但我的头未更新-Java

[英]Merge Two Sorted Lists, but my head is not updated - Java

I am working on "Merge two sorted linked list", but it looks my head has not been updated correctly. 我正在处理“合并两个排序的链表”,但看起来我的头没有正确更新。

To solve this problem, I tried .val, but it doesn't work correctly in while loop. 为了解决此问题,我尝试使用.val,但在while循环中无法正常工作。

Then, the wired thing is that when I tried .next, it works. 然后,有线连接是当我尝试.next时,它可以工作。 I am totally confused. 我很困惑。 I put both code below(the working one and the wrong one), so you will be able to see what I did. 我将两个代码都放在下面(有效的代码和错误的代码),因此您将能够看到我的工作。

Could anybody give me an explanation of why the first is not working, please? 有人可以解释为什么第一个不起作用吗?

Wrong one: 错误之一:

    /**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
/**
 * @param ListNode l1 is the head of the linked list
 * @param ListNode l2 is the head of the linked list
 * @return: ListNode head of linked list
 */
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null)
        return l2;
    else if (l2 == null)
        return l1;

    ListNode result = new ListNode(5);
    ListNode head = result;

    while (l1 != null && l2 != null){
        if (l1.val < l2.val){
            result = l1;
            l1 = l1.next;
        }
        else{
            result = l2;
            l2 = l2.next;
        }
        result = result.next;
    }

    if (l1 == null){
        result = l2;
    }
    else{
        result = l1;
    }

    return head;

    }
}

Working one: 工作之一:

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null)
        return l2;
    else if (l2 == null)
        return l1;

    ListNode result = new ListNode(5);
    ListNode head = result;

    while (l1 != null && l2 != null){
        if (l1.val < l2.val){
            result.next = l1;
            l1 = l1.next;
        }
        else{
            result.next = l2;
            l2 = l2.next;
        }
        result = result.next;
    }

    if (l1 == null){
        result.next = l2;
    }
    else{
        result.next = l1;
    }

    return head.next;

}

The only difference is that I add .next in the second one. 唯一的区别是,我在第二个中添加了.next。

Thank you! 谢谢!

The code uses a dummy node so that after merging, dummy_node.next will end up pointing to the merged list. 该代码使用了一个虚拟节点,因此在合并之后,dummy_node.next将最终指向合并列表。 This simplifies the code so it doesn't have to conditionally deal with an initially empty merged list. 这简化了代码,因此不必有条件地处理最初为空的合并列表。 (In C or C++, a pointer to pointer to node could be used instead of a dummy node, but java doesn't have an equivalent.) The code starts off by setting both result and head as references to the dummy node, then advances result as the lists are merged. (在C或C ++中,可以使用指向节点的指针代替虚拟节点,但是java没有等效的指针。)代码通过将result和head设置为对虚拟节点的引用来开始,然后前进列表合并后的结果。 The working code is returning head.next, which is the original dummy_node.next as intended. 工作代码返回head.next,它是预期的原始dummy_node.next。 The non-working code is returning head, which is a reference to the dummy node, instead of a reference to the merged list. 非工作代码正在返回头,它是对虚拟节点的引用,而不是对合并列表的引用。

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

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