c语言
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
struct ListNode *a= pHead1;
struct ListNode*b= pHead2;
struct ListNode*start2=(struct ListNode*)malloc(sizeof(struct ListNode));
start2->next=NULL;
struct ListNode*start=start2;
if(a==NULL&&b==NULL)
return start2->next;
while(1)
{ if(a==NULL||b==NULL)
break;
struct ListNode *m=start->next=(struct ListNode*)malloc(sizeof(struct ListNode));
int anum=a->val;
int bnum=b->val;
if(anum<=bnum){
m->val=a->val;
a=a->next;
}else{
m->val=b->val;
b=b->next;
}
start=start->next;
}
if(!a){
start->next=b;
}
if(!b){
start->next=a;
}
return start2->next;
}