【问题标题】:How to iteratively add new node in a linked list in Java?如何在Java的链表中迭代地添加新节点?
【发布时间】:2021-12-06 02:13:14
【问题描述】:

我正在尝试从 Leetcode here 编写一个方法,该方法允许合并两个排序的链表(请忽略我需要添加 l2 的值更大的情况这一事实)。到目前为止,我的策略是创建一个名为 mergedList 的新链表,并使用 while 循环迭代地添加新值:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode temp1 = l1;
        ListNode temp2 = l2;
        ListNode curr = null;
        
        ListNode mergedList = null;
        ListNode testpoint = null;
        
        
        if(l1 == null && l2 == null) {
            return null;
        }
        else if(l1 == null && l2 != null) {
            return l2;
        }
        else if(l1 != null && l2 == null) {
            return l1;
        }
        else if(l1.next == null && l2.next == null) {
            if(l1.val <= l2.val) {
                l1.next = new ListNode(l2.val);
                return l1;
            }
            else {
                l2.next = new ListNode(l1.val);
                return l2;
            }
        }
        
        if(temp1.val <= temp2.val) {
            while(temp1 != null) {
                mergedList = new ListNode(temp1.val);
                testpoint = mergedList;
                while(temp2 != null) {
                    if(temp1.next != null) {
                        if(temp1.val <= temp2.val && temp2.val <= temp1.next.val) {
                            curr = temp2;
                            mergedList.next = new ListNode(temp2.val);
                        }
                    }
                    temp2 = temp2.next;
                }
                temp2 = curr;
                temp1 = temp1.next;
            }
        }
        
        System.out.println("");
        System.out.println("");
        
        while(testPoint != null) {
            System.out.println(testPoint.val);
            testPoint = testPoint.next;
        }
        
        
        return mergedList;
    }
}

但是,当我尝试使用 testpoint 作为指向它的指针打印出合并列表时,打印的唯一值是最后一个值 - 为什么?我错过了什么会阻止正确创建新的链表?

【问题讨论】:

  • 看看how to debug small programs。这绝对是调试将帮助您发现方法中的缺陷的情况之一。单步执行您的代码,观察变量并查看代码是否真的按照您的意愿执行。这是早期学习的宝贵技能。

标签: java linked-list singly-linked-list


【解决方案1】:

一些问题:

  • 代码的第二部分创建的所有新节点都分配给mergedListmergeList.next,因此以mergeList 为首的列表不能有超过2 个节点。因此,预计您会进行仅打印一个节点的测试。

  • 内部循环使用temp1.val 创建节点,但是——除了初始情况——从来没有使用temp2.val 创建节点。

  • 嵌套循环很难遵循。使用temp2 = curr;,您可能会多次迭代第二个列表。这应该没有必要。

我认为没有必要在代码的第一部分处理这么多基本案例。其中一些可以在代码的更通用的第二部分中处理。

也没有必要创建temp1temp2 变量。可以使用参数变量l1l2

这是使它工作的一种方法:

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode mergedList = null, curr = null, tail = null;

        while (l1 != null && l2 != null) { // There are still nodes in both lists
            if (l1.val <= l2.val) { // Grab a node from the first list
                curr = new ListNode(l1.val);
                l1 = l1.next;
            } else { // Grab a node from the second list
                curr = new ListNode(l2.val);
                l2 = l2.next;
            }
            if (tail == null) {  // First time only
                mergedList = curr;
            } else {
                tail.next = curr;
            }
            tail = curr; // Keep track of last node in merged list
        }
        // At least one of the input lists has been processed completely.
        // Use the potentially remaining part from the other input list
        if (tail == null) { // One input list was empty
            return l1 != null ? l1 : l2;
        }
        tail.next = l1 != null ? l1 : l2;
        return mergedList;
    }
}

最好不要改变输入列表,但在这种情况下,代码质询允许这种突变,因此没有必要创建新节点:在上面的代码中,您可以将节点创建部分替换为:

        if (l1.val <= l2.val) { // Grab a node from the first list
            curr = l1;
            l1 = l1.next;
        } else { // Grab a node from the second list
            curr = l2;
            l2 = l2.next;
        }

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多