Algorithm question: merge two ordered linked lists

Source: Internet
Author: User

Note: This article is only for learning and communication. For more information, see the source!

Question: it is known that there are two ordered Single-Chain tables, whose header pointers are head1 and head2, respectively. This function combines these two linked lists:

Node * listmerge (node * head1, node * head2)

This algorithm is similar to the merge sort in our sort algorithm. It can only be said to be "very similar" because the idea is the same, but it is different from the merge sort. The difference is as follows:

1. Merge Sorting is for ordered arrays, and here is an ordered linked list;

2. the time complexity of Merge Sorting is O (nlogn), and the worst time complexity here is O (m + n), and the best time is O (min {M, n }).

3. Merging and sorting requires re-applying for space, and there is no need to re-apply for space here. You only need to change the pointer of the linked list node.

The idea of the algorithm here is the same as that of Merge Sorting. They both set one pointer for the two linear tables to be merged and compare the size of the two current pointers, add a small node to the merged linear table and move the current pointer backward. If at least one of the two linear tables is scanned, add the corresponding table to the merged linear table. Here:The difference between a linked list and an array is that a linked list only needs to change the position of the pointer at the end of the current merged sequence, while an array needs to copy the remaining values to the end of the merged table in sequence..

The Recursive Implementation of the algorithm is as follows:

Node * listmerge1 (node * head1, node * head2) // use a recursive method to implement {If (head1 = NULL) return head2; If (head2 = NULL) return head1; node * head = NULL; If (head1-> value 

The non-Recursive Implementation of the algorithm is as follows:

Node * listmerge (node * head1, node * head2) {If (! Head1) return head2; If (! Head2) return head1; node * head = NULL; // The merged header pointer node * P1 = head1; // P1 is used to scan the Linked List 1 node * P2 = head2; // P2 is used to scan the chain table 2if (head1-> value 

The test code is as follows:

# Include <iostream> using namespace STD; struct node {int value; node * Next; node (int v): Value (v) {}};/* Create a linked list, 1-> 2-> 3-> 4-> 5-> 6-> 7 */node * createlist1 () // create an ordered single-chain table 1 {node * head; node * n1 = new node (1); node * N3 = new node (3); node * N5 = new node (5); node * N7 = new node (7 ); node * nodes = new node (9); Head = N1; N1-> next = N3; N3-> next = N5; N5-> next = N7; n7-> next = sequence; Sequence-> next = NULL; return head;} node * createlist2 () // create an ordered single-chain table 2 {node * head; node * n2 = new node (2); node * N4 = new node (4); node * N6 = new node (6 ); node * n8 = new node (8); Head = n2; N2-> next = N4; N4-> next = N6; N6-> next = n8; n8-> next = NULL; return head;} void freelist (node * head) // release the linked list space {If (Head = NULL) {return ;} else {node * temp = head-> next; Delete head; head = temp; freelist (head) ;}} void visitlist (node * head) // element in the traversal chain table, recursively traverse {If (head) {cout 

The test results are as follows:


References ------------- offoffoffer

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.