解题思路

采用递归的方法来进行两个节点的比较,并将最小的放在新建的节点内,重复上述操作,直至两个链表为空。

 * 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 ) {
    if(pHead1==NULL){
        return pHead2;
    }
    if(pHead2==NULL){
        return pHead1;
    }
    struct ListNode *p=NULL;
    if (pHead1->val<pHead2->val){
        p=pHead1;
        p->next=Merge(pHead1->next, pHead2);
    }else{
        p=pHead2;
        p->next=Merge(pHead1, pHead2->next);
    }
    return p;
    // write code here
}