两个有序链表的合并

思想:

 1 LinkList * merge(LinkList* HeadA,LinkList* HeadB){

 2   LinkList* HeadC=NULL,*last=NULL;

 3       while(HeadA!=NULL&&HeadB!=NULL){

 4     if(HeadA->data<HeadB->data)

 5     {

 6       if(HeadC==NULL)

 7          HeadC=last=HeadA;

 8       else

 9       {

10         last->next=HeadA;

11         last=HeadA;

12       }

13           HeadA=HeadA->next;

14     }

15     if(HeadA->data>HeadB->data)

16     {

17       if(HeadC==NULL)

18          HeadC=last=HeadB;

19       else

20       {

21         last->next=HeadB;

22         last=HeadB;

23       }

24           HeadB=HeadB->next;

25     }

26   }

27       if(HeadA!=NULL)

28     last->next=HeadA;

29       if(HeadB!=NULL)

30     last->next=HeadB

31   return HeadC;

32 }

你可能感兴趣的:(链表)