/**
* struct ListNode {
int val;
struct ListNode *next;
};
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
struct ListNode* P,* Q;
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL;
Q = head;
if(!pHead1) return pHead2;
if(!pHead2) return pHead1;
while(pHead1&&pHead2){
if(pHead1->val <= pHead2->val){
P = pHead1;
pHead1 = pHead1->next;
}else{
P = pHead2;
pHead2 = pHead2->next;
}
Q->next = P; // 后插法
Q = P;
}
if(pHead1 != NULL) {
Q->next = pHead1;
} else {
Q->next = pHead2;
}
struct ListNode* nhead = head->next;
free(head);
return nhead;
}
不要忘记保存最后一个结点
if(pHead1 != NULL) {
Q->next = pHead1;
} else {
Q->next = pHead2;
}

京公网安备 11010502036488号