为什么我的合并列表算法不起作用

时间:2019-02-12 22:31:32

标签: java algorithm

我在[这里]有这个问题:https://leetcode.com/problems/merge-two-sorted-lists/

这是我的代码:

function createColumnDefs = (lookup) => ([
{
  field: 'projects',
  headerName: 'Projects',
  filter: 'agSetColumnFilter',
  cellRenderer: 'ListRenderer',
  cellRendererParams: {
    accessor: 'name'
   },
   filterParams: {
     values: _.get(lookup, 'projects', []).map(project => project.name),
     debounceMs: 200
  }
},
{
  field: 'keyword',
  headerName: 'Keyword',
  filter: 'agTextColumnFilter',
  sort: 'asc',
  filterParams: {
    debounceMs: 200
  },
  pinned: 'left',
  minWidth: 250
}
]);

我希望它具有合并列表的正确输出。但是它返回一个空列表。有人可以告诉我我的代码有什么问题吗?

3 个答案:

答案 0 :(得分:1)

您的基本想法是正确的,但是您的算法存在一些问题。

您做的时候两次移动头

    // Set current pointer to l1
    curr = l1;
    // Now curr == l1, both l1 and curr are pointing to the same object,
    // you are changing refence to the l1.next, so you have just missed l1 value
    curr = curr.next;
    // Change to one more element further, so in result your are moving twice
    l1 = l1.next;

因此,您需要分配值,然后进一步移动

此外,您将获得一个设置为NULL的列表指针,而另一个指针将剩下项,在这种情况下,只剩下一项。

考虑到这种情况,您需要添加其余项,具体取决于哪个指针将被设置为空l1l2。列表中只有一个仍然具有项目,因为上一个while循环会将另一个设置为null。

这是算法的修改版本。

public class Main {
    public static void main(String[] args) {
        // Init first list 
        ListNode firstList = new ListNode(new ListNode(new ListNode(null,4),2),1);
        ListNode secondList =new ListNode(new ListNode(new ListNode(null,4),3),1);
        ListNode mergedList = mergeTwoLists(firstList,secondList);
        while(mergedList !=null) {
            System.out.println(mergedList.val);
            mergedList = mergedList.next;
        }
    }
    public static class ListNode {
        public ListNode next;
        public int val;
        public ListNode(ListNode next,int val) {
            this.next = next;
            this.val = val;
        }
        public ListNode() {

        }
    }
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //dummyhead doesn't store anything useful
        ListNode dummyhead = new ListNode();
        ListNode curr = dummyhead;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                curr.next = l1;
                curr = curr.next;
                l1 = l1.next;
            } else {
                curr.next = l2;
                curr = curr.next;
                l2 = l2.next;
            }
        }
        // Add remaining items from first list
        while(l1 !=null) {
            curr.next = l1;
            curr = curr.next;
            l1 = l1.next;
        }
        // Add remaining items from second list
        while(l2 !=null) {
            curr.next = l2;
            curr = curr.next;
            l2 = l2.next;
        }
        return dummyhead.next;    
    }
}

当然可以对其进行优化和重构,但是为了更好地理解,我如上所述对其进行了修改。

答案 1 :(得分:0)

ListNode curr = dummyhead.next;这里的dummyhead.next为null,因为它未指向使curr也为null的对象。

curr = l1;这样,指向null的curr现在指向l1。在这里哪里没有设置dummyhead.next。

所以您可以代替

ListNode curr = dummyhead;
    while(l1 != null && l2 != null) {
        if(l1.val < l2.val) {
            //System.out.println("!!!");
            curr.next = l1;
            curr = curr .next;
            l1 = l1.next;

答案 2 :(得分:0)

您声明一个名为curr的变量,并使用下一个dummyhead的值(为null)启动。然后,给它分配一个新值,但对虚拟头结构没有任何作用。显然结果将是一个空列表,因为您不对结构dummyhead进行任何操作。 您的任务(完全是一项学习任务)是保持与光标的初始结构接触。

通常有助于画一幅画,结构之间如何相互指向,以理清结果的样子。

相关问题