Sword refers to the surface of offer question 17 merge two sorted list

Source: Internet
Author: User

Problem Description :

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

The implementation code is as follows:

#include <stdio.h> #include <stdlib.h> #include <stdbool.h>struct list{int date;struct List *next;}; struct list* domergelist (struct list *p1head,struct list *p2head) {if (p1head==null) return P2head;else if (p2head==null) return p1head;struct List *mergehead = null;if (p1head->date <p2head->date) {mergehead = p1head;mergehead-> Next=domergelist (P1head->next,p2head);} else {mergehead=p2head;mergehead->next=domergelist (p1head,p2head->next);} return mergehead;} int main () {struct list *p1head= (struct list *) malloc (sizeof (struct list)), struct list *p2head= (struct list *) malloc ( sizeof (struct List)); p1head->date=0; p1head->next=null; p2head->date=1; P2head->next=null;int i;struct List *p1temp=p1head,*p2temp=p2head;for (i=1;i<4;i++) {P1Temp->next= (struct List *) malloc (sizeof (struct List)); p1temp->next->date=i; p1temp->next->next=null; p2temp->next= (struct list *) malloc (sizeof (struct list)); p2temp->next->date=i*3; P2temp->next->next=null; p1temp=p1temp->next; P2temp=p2temp->next;} P1temp=p1head,p2temp=p2head;printf ("This was first list:\t"); while (P1temp!=null) {printf ("%d\t", p1temp->date); P1temp=p1temp->next;} printf ("\nthis is second list:\t") and while (P2temp!=null) {printf ("%d\t", p2temp->date); P2temp=p2temp->next;} printf ("\nthis is merge List:\t"), the struct list *head = Domergelist (P1head,p2head), while (Head!=null) {printf ("%d\t", Head->date); Head=head->next;} printf ("\ndomerge done!\n"); return 0;}

References
Sword means offer

Notes
Reprint Please specify source: http://blog.csdn.net/wsyw126/article/details/51372134
WSYW126

Sword refers to the surface of offer question 17 merge two sorted list

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.