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

合并两个排序的链表

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

/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2) {
    // write code here
    struct ListNode* t = NULL; struct ListNode H; struct ListNode* h1 = &H;
    h1->next = pHead1; struct ListNode* temp = pHead2;

    if (pHead1 == NULL) return pHead2;
    if (pHead2 == NULL) return pHead1;

    while (h1->next != NULL){
        if (temp->val < h1->next->val){
            t = temp; temp = h1->next; 
            h1->next = t; h1 = h1->next;
        }
        else h1 = h1->next;
    }
    h1->next = temp;

    return H.next;
}
#数据结构编程链表#
全部评论

相关推荐

点赞 评论 收藏
分享
01-07 15:50
四川大学 Java
看日出看日落:好好背八股,做算法。我身边跟你bg差不多的基本都大厂暑期
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务