Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在合并K排序的链接列表中循环_C++_Sorting_Pointers_Merge - Fatal编程技术网

C++ 在合并K排序的链接列表中循环

C++ 在合并K排序的链接列表中循环,c++,sorting,pointers,merge,C++,Sorting,Pointers,Merge,我的合并k排序列表算法使用分治,并使用合并2列表算法作为过程的辅助工具 问题在于在迭代过程中创建了一个循环,我不知道为什么 我将代码追溯到发生这种情况的确切地点,但我仍然无法识别问题所在 class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2, bool debug=false) { ListNode *head = new ListNode(-1);

我的合并k排序列表算法使用分治,并使用合并2列表算法作为过程的辅助工具

问题在于在迭代过程中创建了一个循环,我不知道为什么

我将代码追溯到发生这种情况的确切地点,但我仍然无法识别问题所在

class Solution {
public:

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2, bool debug=false) 
    {
        ListNode *head = new ListNode(-1);
        ListNode *curr = head;

        while(l1 && l2) 
        {   

          if(l1->val <= l2->val)
            {
                curr->next = l1;
                l1 = l1->next;
            }
            else
            {
                curr->next = l2; 
                l2 = l2->next;

            }
            curr = curr->next;

        }
        // some list may be still populated
        l1 != NULL ? curr->next = l1 : curr->next = l2;
        return head->next;
    }


    ListNode* mergeKLists(std::vector<ListNode*>& lists) 
    {
        // approach of divide and conquer
        int size = lists.size();
        int interval = 1;
        int tmp_val = 1;
        bool debug= false;
        while(interval < size)
        {
            for(int i=0; i<size-interval; i*=2)
            {

                lists[i] = mergeTwoLists(lists[i], lists[i+interval], debug=debug);
                if (i==0)
                    i++;
            }
            interval*=2;
        }

        if (size)
            return lists[0];
        else
        {
            ListNode* ret=NULL;
            return ret;
        }

    }


};

有人能给我一些提示吗?

看来您的
合并两个列表
修改了传递给它的两个列表,这样它们就可以从共享节点中出来。如果您确保将其中一个放在一边,并且不再使用它,那么这不会是一个问题(至少不是一个大问题)

显然,这正是您在
mergeKLists
中处理索引的目的,但是存在一个错误:您错误地增加了
i
。重复使用不应该重复使用的列表,在共享一个节点的两个列表上调用
mergetowlist
,它会在列表中创建一个循环并永远迭代

快速而肮脏的解决方案是修复
mergeKLists
中的索引算法。更深层次的解决方案是更加小心地使用
mergeTwoList
中的指针,这样两个不相交的列表就不相交了。

您以前听说过
                curr->next = l2;  
                l2 = l2->next;