/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
typedef struct ListNode Node;
Node* head=(Node*)malloc(sizeof(Node));//用来储存数据
Node* tail=head;//用来引出数据
if(pHead1==NULL){
return pHead2;
}
if(pHead2==NULL){
return pHead1;
}//验证是否存在空指针
while(pHead1&&pHead2){
if(pHead1->val<pHead2->val){//比较数据的大小
tail->next=pHead1;//引出下一个节点
tail=pHead1;
pHead1=pHead1->next;//引出下一个数据
}else{
tail->next=pHead2;
tail=pHead2;
pHead2=pHead2->next;
}
}
if(pHead1){
tail->next=pHead1;
}
if(pHead2){
tail->next=pHead2;
}//如果有任意一个链表结束,则下一个指向另一个链表
return head->next;//返回head
}