题解 | #合并两个排序的链表#

合并两个排序的链表

https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

/**
 * 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 && !pHead2)
    {
        return NULL;
    }
    if(!pHead1 )
    {
        return pHead2;
    }
    if(!pHead2)
    {
        return pHead1;
    }
    struct ListNode * Head ,*p1,*p2;
    if(pHead1 -> val > pHead2 -> val)
    {
        Head = pHead2;
        pHead2 = pHead1;
    }
    else 
    {
        Head = pHead1;
    }
    p1 = Head -> next;
    p2 = Head;
    while(p1 && pHead2)
    {
        if(p1 -> val > pHead2-> val)
        {
            p2 -> next = pHead2;
            pHead2 = pHead2 -> next;
        }
        else 
        {
            p2 -> next = p1;
            p1 = p1 -> next;
        }
        p2 = p2 -> next;
    }
    if(p1)
    {
        p2 -> next = p1;
    }
    else
    {
        p2 -> next = pHead2;
    }
    return Head;
    
    
    
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-23 14:13
这是聊岔撇了吗,相同的话问了两遍
吴offer选手:上下文切换这一块
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务