题解

合并两个有序的单链表

http://www.nowcoder.com/questionTerminal/98a51a92836e4861be1803aaa9037440

list_node * merge_list(list_node * head1, list_node * head2)
{
    //////在下面完成代码
    list_node* res= new list_node();
    list_node* cur= res;
    while(head1 != nullptr && head2 != nullptr) {
        if(head1->val < head2->val) {
            list_node* tmp= new list_node();
            tmp->val = head1->val;
            tmp->next =nullptr;
            cur->next = tmp;
            cur = tmp;
            head1 = head1->next;
        } else if(head1->val >= head2->val) {
            list_node* tmp= new list_node();
            tmp->val = head2->val;
            tmp->next =nullptr;
            cur->next = tmp;
            cur = tmp;
            head2 = head2->next;
        }
    }
    if(head1 != nullptr) cur->next = head1;
    if(head2 != nullptr) cur->next = head2;
    return res->next;
}
全部评论

相关推荐

湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
07-11 11:15
中南大学 Java
好可爱的hr姐姐哈哈哈哈
黑皮白袜臭脚体育生:兄弟们貂蝉在一起,吕布开了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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