Merge two sequential linked lists with C ++ Algorithms

Source: Internet
Author: User

Merge two sequential linked lists with C ++ Algorithms

Question: merge two sorted linked lists

Method 1:

Two linked lists

For example, linked list 1: 1-> 3-> 5-> 7-> 9
Linked List 2: 2-> 4-> 6-> 8-> 10

Like merging two arrays, the first node of linked list 1 is compared with the first node of linked list 2. If the value of the first node of linked list 1 is greater than that of the second node of the linked list,
If the head node of linked list 2 is the head node of the merged linked list, the head node of linked list 1 continues with the second node of linked list 2 (the head node of the remaining linked list 2)
For comparison, but after a linked list is traversed, if another linked list has not been traversed, because the linked list is originally sorted
The tail node points to the header node that has not traversed the linked list.

For example:

Linked List 1: 1, 3, 5, 23, 34;
Linked List 2: 2, 4, 6, 8, 10;
After traversing, the linked list is 3: 1, 2, 3, 4, 8, and 10. Now linked list 2 has been traversed and the while LOOP exits, but the remaining linked list 1 has
Now let the tail node 10 of linked list 3 link to the header node 23 of the remaining linked list.

<Span style = "color: #000000;"> // merged linked list. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_Data; ListNode * m_pNext; ListNode (int value, ListNode * next = NULL): m_Data (value ), m_pNext (next) {}};/* two linked lists, such as the Linked List 1: 1-> 3-> 5-> 7-> 9 Chain List 2: 2-> 4-> 6-> 8-> 10. The same as merging two arrays, the head node of linked list 1 is compared with the head node of linked list 2, if the value of the first node in the linked list is greater than the value of the two contacts in the linked list, the first node in the Chain List 2 is the first node in the merged linked list, the first node of linked list 1 continues to be compared with the second node of linked list 2 (the first node of the remaining linked list 2). However, after a linked list is traversed, if the other linked list is not completely traversed, because the linked list is originally sorted, let the tail node of the merged linked list point to the header node of the list that has not been traversed. For example: Linked List 1: 1, 3, 5, 23, 34; linked list 2: 2, 4, 6, 8, 10; After traversing, the linked list is, 2, 3, 4, 8, and 10. Now linked list 2 has been traversed, And the while LOOP exits, however, the remaining linked list 1 has and 34. At this time, the tail node 10 of linked list 3 is connected to the header node 23 of the remaining linked list. */ListNode * MergeList2 (ListNode * head1, ListNode * head2) {if (head1 = NULL) {return head2;} else if (head2 = NULL) {return head1;} ListNode * MergeHead = NULL; if (head1-> m_Data 


Method 2:

/*

We analyze the process of two linked lists. First, starting from merging the head nodes of two linked lists, the value of the head node of linked list 1 is smaller than the value of the head node of linked list 2. Therefore, the head node of linked list 1 is
It is to merge the first node of the linked list, continue to merge the remaining linked list, and the remaining nodes in the two linked lists are still sorted. Therefore, the process of merging the two linked lists is the same, we can compare the two headers.
Value. At this time, the value of the first node of linked list 2 is smaller than the value of the first node of linked list 1. Therefore, the first node of linked list 2 is the header node that merges the remaining linked list, the tail node of the linked list obtained when the node is merged with the linked list.
Link up

According to the above analysis, we can see that the steps for each merge are the same, so we think of recursion.

*/

// Merge linked list. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_Data; ListNode * m_pNext; ListNode (int value, ListNode * next = NULL): m_Data (value ), m_pNext (next) {}};/* we analyze the process of two linked lists. First, we start from merging the header nodes of the two linked lists, the value of the head node of linked list 1 is smaller than the value of the head node of linked list 2. Therefore, the head node of linked list 1 is the head node of the merged linked list, and the remaining linked list is merged, the remaining nodes in the two linked lists are still sorted. Therefore, the steps for merging two linked lists are the same. We can compare the values of the two header nodes, at this time, the value of the first node of linked list 2 is smaller than the value of the first node of linked list 1. Therefore, the first node of linked list 2 is the header node that merges the remaining linked list, we link this node to the tail node of the linked list obtained when the linked list is merged. According to the above analysis, we can see that the steps for each merge are the same. So we think of recursion. */ListNode * MergeList (ListNode * pHead1, ListNode * pHead2) {if (pHead1 = NULL) {return pHead2;} else if (pHead2 = NULL) {return pHead1 ;} listNode * pMergeHead = NULL; if (pHead1-> m_Data <pHead2-> m_Data) {pMergeHead = cursor; pMergeHead-> m_pNext = MergeList (pHead1-> m_pNext, pHead2 );} else {pMergeHead = pHead2; pMergeHead-> m_pNext = MergeList (pHead1, pHead2-> m_pNext);} return pMergeHead;} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pHead1 = new ListNode (1); ListNode * pCur = pHead1; for (int I = 3; I <10; I ++ = 2) {ListNode * tmpNode = new ListNode (I); pCur-> m_pNext = tmpNode; pCur = tmpNode;} ListNode * pHead2 = new ListNode (2); pCur = pHead2; for (int j = 4; j <10; j + = 2) {ListNode * tmpNode = new ListNode (j); pCur-> m_pNext = tmpNode; pCur = tmpNode ;} listNode * head = MergeList2 (pHead1, pHead2); while (head) {cout 


 

After reading this question, you can use recursion to enclose the code in the last merged array:

<Span style = "color: #000000;"> // MergeArray. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std; // Recursive Method void MergeArray2 (int a [], int aCount, int B [], int blen) {int len = aCount + blen-1; aCount --; blen --; if (aCount <0) {while (blen> = 0) {a [len --] = B [blen --];} return;} if (a [aCount]> B [blen]) {a [len] = a [aCount]; mergeArray2 (a, aCount, B, ++ blen);} else {a [len] = B [blen]; MergeArray2 (a, ++ aCount, B, blen) ;}} void MergeArray (int a [], int aCount, int B [], int blen) // aCount is the actual (narrow) Length of array, blen is the actual length of array B {int len = aCount + blen-1; // The length of the merged array is the generalized length of array a aCount --; blen --; while (aCount> = 0 & blen> = 0) {if (a [aCount]> = B [blen]) {a [len --] = a [aCount --];} else {a [len --] = B [blen --] ;}} while (blen> = 0) {a [len --] = B [blen --] ;}} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 6, 8,, 0,}; int B [] = {, 9}; MergeArray2 (a, 5, B, 5); for (int I = 0; I <sizeof (a)/sizeof (a [0]); I ++) {cout <a [I] <"" ;}getchar (); return 0 ;}</span>


 

I personally feel that it is not good to use recursion to merge arrays, because it is a little troublesome to consider that if one array has not been completely traversed by another array, and if it is a linked list, one linked list has been calendar,

If this linked list is empty, another linked list will be returned, that is, the part of the previously merged linked list that is not completely traversed!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.