题解

合并两个有序的单链表

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;
}
全部评论

相关推荐

弦五Strings:他之所以会举报你代课是因为在这种人眼里正常上课就是正义代课就是邪恶,典型二极管思维,处理方法就是私下沟通,你就说你自己家里经济困难或者家里父母生病什么之类的,需要去打工挣钱,用尽孝的正义对冲他认为的上课的正义,他可能就妥协了。
我的实习日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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