简体   繁体   English

合并两个排序的链表

[英]Merging two sorted linkedlists

I am trying to write a program to merge two sorted linked lists.But the last element does not get inserted at all.我正在尝试编写一个程序来合并两个排序的链表。但最后一个元素根本没有插入。 The code seems to fail when try to insert a node after the last node in listA.尝试在 listA 中的最后一个节点之后插入一个节点时,代码似乎失败了。 Any help would be appreciated.任何帮助,将不胜感激。

  /*
      Merge two sorted lists A and B as one linked list
      Node is defined as 
      struct Node
      {
         int data;
         struct Node *next;
      }
    */
    Node* MergeLists(Node *headA, Node* headB)
    {
      // This is a "method-only" submission. 
      // You only need to complete this method 
      Node *prev;
      Node *currentA=headA;
      Node *currentB=headB;
      Node *next;
        if(headA==NULL)
            {
            return headB;
        }
        if(headB==NULL)
            {
            return headA;
        }
      while(currentA!=NULL && currentB!=NULL)
          {
        if(currentB->data < currentA->data)
            {
            Node *new_node=new Node;
            new_node->data=currentB->data;
            headA=new_node;
            new_node->next=currentA;
            currentA=new_node;
            currentB=currentB->next;
        }
        else if(currentB->data > currentA->data && currentB->data < currentA->next->data)
            {
            Node *new_node=new Node;
            new_node->data=currentB->data;
            next=currentA->next;
            prev=currentA;
            new_node->next=next;
            prev->next=new_node;
            currentA=new_node;
            currentB=currentB->next;

        }
        else
            {
            currentA=currentA->next;

        }
          if(currentA->next==NULL)
              {
              Node *new_node=new Node;
              new_node->data=currentB->data;
              prev=currentA;
              prev->next=new_node;
              new_node->next=NULL;
              currentA=new_node;
              currentB=currentB->next;
          }
      }
        return headA;
    }

Input:输入:

1 (No of test cases)
2 (Length of list A)
2 4
4 (Length of list B)
1 3 5 7

Expected output:预期输出:

1 2 3 4 5

Actual Output:实际输出:

Runtime error

I think the real problem is with your input.我认为真正的问题在于您的意见。

1 (No of test cases) 1(测试用例数)

2 (Length of list A) 2(列表A的长度)

2 4 2 4

2 (Length of list B) 2(列表B的长度)

1 3 5 1 3 5

The length of list B should be 3. As it is mentioned 2 in your input.列表 B 的长度应为 3。正如您在输入中提到的 2。 Your output contains only 2 elements from your list B.您的输出仅包含列表 B 中的 2 个元素。

Kindly check with the correct input which is as follows:请检查正确的输入如下:

1 (No of test cases) 1(测试用例数)

2 (Length of list A) 2(列表A的长度)

2 4 2 4

3 (Length of list B) 3(列表B的长度)

1 3 5 1 3 5

When the code reaches the end of list A or list B, it's missing the code to copy the remainder of list A or list B. I'm also wondering about the check using currentA->next after setting currentA = currentA->next, which could be NULL.当代码到达列表 A 或列表 B 的末尾时,它缺少复制列表 A 或列表 B 的其余部分的代码。我还想知道在设置 currentA = currentA->next 后使用 currentA->next 进行检查,这可能是NULL。

The code is also complex, if (currentB->data < currentA->data), you move or copy a node from list B, else you move or copy a node from list A, there's no need for the complex else if after if (currentB->data < currentA->data), just the else is good enough.代码也很复杂,如果(currentB->data < currentA->data),你从列表B中移动或复制一个节点,否则你从列表A中移动或复制一个节点,则不需要复杂的else if after (currentB->data < currentA->data),只要 else 就足够了。

The code could be simplified:代码可以简化:

    prev = NULL;    /* this will be sorted list */
    /* ... */
        if(prev == NULL){     /* if first element */
            prev = new_node;
            next = new_node;
        } else {
            next->next = new_node;
            next = new_node;
        }
    /* ... */
    next->next = NULL;        /* set end of list */

You might want to consider changing the name of prev to head and next to tail.您可能需要考虑将 prev 的名称更改为 head 和 next to tail。

Usually a mergelists function moves nodes from list A and list B into the sorted output list, as opposed to creating a list of copies of the nodes.通常,mergelists 函数将节点从列表 A 和列表 B 移动到排序的输出列表中,而不是创建节点的副本列表。 In this case, new Node would never be used, and the existing nodes from list A and list B would be linked together in order.在这种情况下,将永远不会使用新节点,并且列表 A 和列表 B 中的现有节点将按顺序链接在一起。

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

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