Merge two ordered linked lists

Source: Internet
Author: User

title : Enter two incrementally sorted lists, merge the two linked lists, and make the nodes in the new list continue to be sorted in ascending order. For example, enter two linked lists 1,3,5 and 2,4,6 respectively, the combined list is 1,2,3,4,5,6.

The linked list node is defined as follows:

typedef struct LISTNODE{INT val;struct ListNode *p_next;} NODE, *pnode;

Get this topic and we'll analyze it: first we analyze the process of merging two linked lists, and we start from the beginning. Suppose there are two of these linked lists:


The head node of the list 1 is less than the head node of the linked list 2, so the head node of the linked list 1 will act as the new merged head node, as follows:

We then continue to merge the remaining nodes in the two linked lists, which are the nodes in the box. The remaining nodes are sorted separately, so the steps to merge the two lists are the same as before. is still compared to the value of two head nodes, because the value of the head node of the chain table 2 is less than the value of the head node of the linked list 1, so the head node of the linked list 2 becomes the new head node after the remaining linked list is merged, and we connect it behind the previously identified head node:

Each time we get a new head node and connect to the end of the new list, the remaining list is still ordered, so the merging steps are the same as before. This is a typical recursive process. So I decided to implement it first with a recursive return.

But before you do, one thing to note is that when the first list that is passed in is empty. , that is, the head node pointer is null, we want to merge it with the linked List 2, obviously the result of the merger is linked List 2, the same, the linked list 2 is empty, the linked list is linked to the list 1; If two linked lists are empty lists, then the merge is still an empty list. Now that we've figured out specific ideas and robustness considerations, we can start writing code:

Pnode merge_two_list_recur (Pnode head1, Pnode head2) {Pnode new_head = null;if (null = = Head1) return Head2;else if (null = = HEAD2) return head1;if (Head1->val <= head2->val) {new_head = Head1;new_head->p_next = Merge_two_list_recur ( HEAD1->P_NEXT,HEAD2);} Else{new_head = Head2;new_head->p_next = Merge_two_list_recur (Head1, head2->p_next);} return new_head;}

Finally, we can implement this algorithm in a non-recursive way:

Pnode merge_two_list (Pnode head1, Pnode head2) {Pnode new_head = NULL, TMP = NULL, next = Null;int flag = 0;/*if (NULL = = h EAD1) return Head2;else if (null = = head2) return head1;*/while (null! = Head1 && NULL! = head2) {if (Head1->val & lt;= head2->val) {tmp = HEAD1;HEAD1 = head1->p_next;} else{tmp = Head2;head2 = Head2->p_next;} if (flag) {Next->p_next = Tmp;next = Next->p_next;} else//only come in once {new_head = Tmp;next = New_head;flag = 1;}} if (NULL! = head1) {next->p_next = Head1;} else if (NULL! = head2) {next->p_next = head2;} return new_head;}

Still not difficult, here is not to repeat.


Merge two ordered linked lists

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.