Two singly linked lists are arranged in increased order.
If a->data is greater than b->data, h-> Next points to B, and h-> Next points to A. If either a or B is not empty, then h->next points to a or B
#include <stdio.h> #include <stdlib.h> typedef struct d{ int data; struct d *next; }node; node *Creat(int *a,int len) { node *head=(node *)malloc(sizeof(node)); head->data=NULL; node *ret=head; for(int i=0; i<len; i++) { node *s=(node *)malloc(sizeof(node)); s->data=a[i]; s->next=NULL; head->next=s; head=s; } return ret; } node *merge(node *a,node *b) { node *ret=a; // return node *q,*h; h=ret; a=a->next; q=b; b=b->next; free(q); // delete b header for(; a! =NULL&&b! =NULL;) If (a->data>b->data) {// If (a->data> B ->data) {// if(a->data> B ->data) {// if(a->data> B ->data) {// if(a->data> B ->data) {// if(a->data> B ->data) {// if(b-> next=b; b=b->next; h=h->next; }else { h->next=a; a=a->next; h=h->next; } } h->next=NULL; if(a! =NULL) { h->next=a; } if(b! =NULL) { h->next=b; } return ret; } void Print(node *h) { for(h=h->next; h; h=h->next) { printf("%d->",h->data); }} int main() {int a[]={1,3,5,7,9}; ,4,6,8,10,12,14,16 int [] b = {2}; int len1=sizeof(a)/sizeof(a[0]); int len2=sizeof(b)/sizeof(b[0]); node *nodea=Creat(a,len1); node *nodeb=Creat(b,len2); node *ret=merge(nodea,nodeb); Print(ret); }Copy the code