Merge two sorted single-linked lists

Source: Internet
Author: User

Topic

Enter a list of two ascending orders, merge the two linked lists, and make the nodes in the new list continue to be sorted in ascending order.

Analysis

Merge single linked list, need to find the head node, compared with two linked list head node, determine the head node, and then determine the head node next node, loop recursive as the previous operation to determine the location of each node, while considering the boundary conditions, if the two linked list is empty, then there is no need to merge, that is, empty list, if a linked list is empty, The other is not empty and returns a list that is not empty. The specific analysis process can be seen in the following example:

"Test Code"

#include<Stdio.H>#include<Stdlib.H>#include<Stack>typedef int DATA_TYPE;TYPEDEF struct Node node_t;//Assign individual names to struct node node_ttypedef struct NODE*Node_ptr;//Give the struct node* an individual name node_ptrtypedef struct node{Data_typeData; struct Node*Node_next;//node_next is a pointer to a struct that tells the pointer to address it to a struct-type address};//List initializationnode_t*Init () {node_ptr p; P=(node_t*) malloc (sizeof (node_t)); P -Node_next= NULL;returnP;}//Insert a node behind a linked listnode_t*Insert_back (Node_ptr p, data_typeData) {Node_ptr pnew=(node_t*) malloc (sizeof (node_t)); Pnew -Node_next= NULL; Pnew -Data = Data; P -Node_next=Pnew;returnPnew;} node_t*Merge (Node_ptr List1_head, node_ptr list2_head) {if(List1_head== NULL)returnList2_head;Else if(List2_head== NULL)returnList1_head; Node_ptr Merge_head= NULL;if(List1_head -Data <List2_head -Data) {Merge_head=List1_head; Merge_head -Node_next=Merge (List1_head -Node_next,list2_head); }Else{Merge_head=List2_head; Merge_head -Node_next=Merge (List1_head, List2_head -Node_next); }returnMerge_head;}//Normal printingvoidPrint (Node_ptr p) {if(!P) {printf ("No data, you think too much");return; } node_ptrList =P while(List -Node_next!= NULL) {printf ("%d",List -Data);List = List -Node_next; } printf ("%d",List -Data); printf"\ n");}voidMain () {node_ptr pnode1,pnode2, list1,list2; Pnode1=Init (); Pnode2=Init (); List1=Pnode1; List2=Pnode2; Pnode1=Insert_back (Pnode1,1); Pnode1=Insert_back (Pnode1,3); Pnode1=Insert_back (Pnode1,5); Pnode2=Insert_back (Pnode2,2); Pnode2=Insert_back (Pnode2,4); Pnode2=Insert_back (Pnode2,6); printf"single-Link table 1 is:"); Print (List1 -Node_next); printf"Its head node element is:%d\n", List1 -Node_next -Data); printf"single-Link table 2 is:"); Print (List2 -Node_next); printf"Its head node element is:%d\n", List2 -Node_next -Data); printf"\ n"); node_t*Merge_list=Merge (List1 -Node_next, List2 -Node_next); printf"Merge single-linked list order:");    Print (merge_list); printf"head node element:%d\n", merge_list -Data); printf"\ n");}

Output

Merge two sorted single-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.