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

合并两个排序的链表

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
    struct ListNode* temp1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* temp2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* result = (struct ListNode*)malloc(sizeof(struct ListNode) * 1001);

    temp1 = pHead1;
    temp2 = pHead2;

    struct ListNode* cur ;
    // result = cur;
    cur = result;
    while((temp1 != NULL) || (temp2 != NULL))
    {

        if((temp1 != NULL) && (temp2 != NULL))
        {

            if(temp1->val <= temp2->val)
            {
                printf("111  p1 %d  p2 %d  cur %d\n",temp1->val, temp2->val, cur->val);
                cur->next = temp1;
                cur = temp1;
                temp1 = temp1->next;
            } else {
                printf("222  p1 %d  p2 %d cur %d\n",temp1->val, temp2->val, cur->val);
                cur->next = temp2;
                cur = temp2;
                temp2 = temp2->next;
            }
        } else if((temp1 == NULL)) {
            cur->next = temp2;
            cur = temp2;
            temp2 = NULL;
        } else if((temp2 == NULL)) {
            cur->next = temp1;
            cur = temp1;
            temp1 = NULL;
        }
    }
                printf("=== %d\n", result->val );

    return result->next;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
面试尴尬现场
点赞 评论 收藏
分享
牛客84809583...:举报了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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