合并两个排序的链表(常考)

来源:互联网 发布:营口港荣数据大平台 编辑:程序博客网 时间:2024/05/16 09:34
//剑指offer递归实现ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    {         ListNode*  p= NULL;        if(pHead1 == NULL)            return pHead2;        if(pHead2 == NULL)            return pHead1;                if(pHead1->val <= pHead2->val){            p = pHead1;            p->next = Merge(pHead1->next, pHead2);        } else {            p = pHead2;            p->next = Merge(pHead1, pHead2->next);        }        return p;        /*         if(pHead1 == NULL)            return pHead2;        if(pHead2 == NULL)            return pHead1;                 ListNode* pNext = NULL;         ListNode* result = NULL;                 while(pHead1 != NULL && pHead2 != NULL){            if(pHead1->val <= pHead2->val)            {                if(result == NULL)                {                    pNext = result = pHead1;                } else                 {                    pNext->next = pHead1;                    pNext = pNext->next;                }                pHead1 = pHead1->next;            } else {                if(result == NULL)                {                    pNext = result = pHead2;                } else                 {                    pNext->next = pHead2;                    pNext = pNext->next;                }                pHead2 = pHead2->next;            }        }                 if(pHead1 == NULL)        {            pNext->next = pHead2;        }        if(pHead2 == NULL)        {            pNext->next = pHead1;        }                 return result;*/    }        


0 0