/**
* 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
if(pHead1 == NULL) return pHead2;
if(pHead2 == NULL) return pHead1;
//创建虚拟节点,简化操作
struct ListNode * dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy ->val = -1;
dummy ->next = NULL;
struct ListNode *curr = dummy;
while(pHead1 != NULL && pHead2 != NULL){
if(pHead1 ->val <= pHead2 ->val){
curr ->next = pHead1;
pHead1 = pHead1 ->next;
}else{
curr ->next = pHead2;
pHead2 = pHead2 -> next;
}
curr = curr ->next;
}
if(pHead1 != NULL){
curr ->next = pHead1;
}
if(pHead2 != NULL){
curr ->next = pHead2;
}
struct ListNode *result = dummy ->next;
free(dummy);
return result;
}