简体   繁体   English

合并两个排序的链表

[英]Merge two sorted linked lists

I'm trying to merge two sorted linked lists. 我正在尝试合并两个排序的链表。 Here I'm just trying to implement my own algorithm. 在这里,我只是试图实现自己的算法。 Of course there are many solutions given on internet. 当然,互联网上有很多解决方案。 The code is : 代码是:

Node* MergeLists(Node *headA, Node* headB)
{
    int countA = 0, countB = 0;
    while(headA){countA++; headA = headA->next;}
    while(headB){countB++; headB = headB->next;}
    Node *res, *tres;
    res = new Node();
    res->next = NULL;
    tres = res;
    for(int i = 0; i < countA+countB-1; i++)
    {
        Node* temp = new Node();
        temp->next = NULL;
        tres->next = temp;
        tres = tres->next;
    }
    while(headA != NULL && headB != NULL)
    {
        if(headA->data > headB->data)
        {
            res->data = headB->data;
            res = res->next;
            headB = headB->next;
        }
        else if(headA->data < headB->data)
        {
            res->data = headA->data;
            res = res->next;
            headA = headA->next;
        }
    }
    while(headA)
    {
        res = headA;
    }
    while(headB)
    {
        res = headB;
    }
    return res;
}

This is just a function which returns the head address of merged linked list. 这只是一个返回合并链表头地址的函数。

Consider this input / output example : 考虑以下输入/输出示例:

Input (stdin):

3
4
1 3 5 6
3
2 4 7
1
15
1
12
0
2
1 2

My Output (stdout)

0 0 0 0 0 0 0
0 0
0 0

Expected Output
1 2 3 4 5 6 7
12 15
1 2

So, my output prints all zero. 因此,我的输出显示所有零。 This is due to the problem in this segment of code : 这是由于这段代码中的问题:

Node *res, *tres;
    res = new Node();
    res->next = NULL;
    tres = res;
    for(int i = 0; i < countA+countB-1; i++)// this is creating a new linked list.
    {
        Node* temp = new Node();
        temp->next = NULL;
        tres->next = temp;
        tres = tres->next;
    }

I think the linkage between the tres and res is not properly happening. 我认为Tres和res之间的联系没有正确发生。 Can you please tell me how to correct this problem. 您能告诉我如何解决此问题吗?

Update : 更新:

Node* MergeLists(Node *headA, Node* headB)
{

    int countA = 0, countB = 0;
    Node* tempA, *tempB;
    tempA = headA; tempB = headB;
    while(headA){countA++; tempA = tempA->next;}
    while(headB){countB++; tempB = tempB->next;}
    Node *res, *tres;
    res = new Node();
    res->next = NULL;
    tres = res;
    for(int i = 0; i < countA+countB-1; i++)
    {
        Node* temp = new Node();
        temp->next = NULL;
        tres->next = temp;
        tres = tres->next;
    }
    while(headA != NULL && headB != NULL)
    {
        if(headA->data > headB->data)
        {
            res->data = headB->data;
            res = res->next;
            headB = headB->next;
        }
        else if(headA->data < headB->data)
        {
            res->data = headA->data;
            res = res->next;
            headA = headA->next;
        }
    }
    if(headA)
    {
        res= headA;
        //res = res->next;
        //headA = headA->next;
    }
    if(headB)
    {
        res = headB;
        //res = res->next;
        //headB = headB->next;
    }
    return res;
}

This time ~ no response on stdout ~ 这次~ no response on stdout ~

Issue is with this part of the code. 问题出在这部分代码。

while(headA){countA++; headA = headA->next;}
while(headB){countB++; headB = headB->next;}

After this loop is executed, both headA & headB are pointing to NULL; 执行此循环后,headA和headB都指向NULL; So when 所以什么时候

while(headA != NULL && headB != NULL)

loop starts, it doesn't even enter the loop. 循环开始,甚至没有进入循环。 Since this loop is responsible for assigning values and it doesn't enter this loop. 由于此循环负责分配值,因此不会进入此循环。 Hence all your values are set to default 0. 因此,所有值都设置为默认值0。

As @Slava mentioned you don't even need this loop. 正如@Slava提到的,您甚至不需要此循环。 As your directly iterating over Nodes and can stop when NULL occurs. 由于直接在Node上进行迭代,因此在发生NULL时可以停止。

Create a temp pointer and use that pointer to calculate countA and countB. 创建一个临时指针,并使用该指针来计算countA和countB。

Something like 就像是

Node* temp;
temp = headA;
while(temp){countA++; temp = temp->next;}
temp = headB;
while(temp){countB++; temp = temp->next;}

Also, this may result in infinite loop. 同样,这可能会导致无限循环。 Please increment the Node inside the loop. 请在循环内增加节点。 or just change the condition to an if, instead of while. 或只是将条件更改为if(而不是一会儿)。

    while(headA)
    {
        res = headA;
    }
    while(headB)
    {
        res = headB;
    }

Update - 更新-

Another Issue: Your returning res after this computation, which is pointing to the last element. 另一个问题:计算之后您返回的res指向最后一个元素。 Hence the output would be just one digit. 因此,输出将仅为一位。

What you can do is 你能做的是

tres = res;
for(int i = 0; i < countA+countB-1; i++)
{
    ...
}
tres = res; // Add this line, so your keeping track of the original head
while(headA != NULL && headB != NULL)

And finally return tres; 最后return tres; instead of return res; 而不是返回res; With this you'll be returning the original head. 有了这个,您将返回原来的头。

You do not need to count elements, precreate result list and use so many loops, do everything in one loop: 您无需计算元素,预先创建结果列表并使用如此多的循环,而无需在一个循环中进行所有操作:

Node* MergeLists(Node *headA, Node* headB)
{
    Node *res = nullptr;
    Node **ptr = &res;
    while( headA || headB  ) {
        Node *curr = new Node;
        curr->next = nullptr;
        *ptr = curr;
        ptr = &curr->next;
        if( headB == nullptr || ( headA && headA->data < headB->data ) ) {
            curr->data = headA->data;
            headA = headA->next;
        } else {
            curr->data = headB->data;
            headB = headB->next;
        }
    }
    return res;
}

Note: you should not return raw pointer from a function - it can easily lead to a memory leak. 注意:您不应该从函数返回原始指针-它很容易导致内存泄漏。 You should use smart pointer instead. 您应该改用智能指针。

节点应包含某些内容-您正在创建一个新节点并将其分配到链的末尾,但根本不保存任何值,因此该值可能只是默认为零。

        res = res->next; <---

while(headA)
{
    res = headA;
}
while(headB)
{
    res = headB;
}
return res;

you returning res that is pointer to the end instead of the head. 您返回的res是指向末尾而不是头部的指针。 Plus while(headB/headA) suppose to add reminder at the end of list but actually will loop infinite because no increment of the pointer. 再加上while(headB / headA)假设在列表的末尾添加提醒,但实际上将循环无限,因为没有指针的增量。

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

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