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

合并两个排序的链表

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

struct ListNode* Merge(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
//---------------------------两个链表都为空
if(head1==NULL && head2==NULL)
return head1;

//---------------------------将head2拼接到head2
struct ListNode*  p =head1;
while(p->next != NULL)
    p=p->next;
p->next = head2;
//----------------------------------
struct ListNode* temp =NULL;
struct ListNode* pp = NULL;
int ch;//交换值

//---------------------------冒泡排序
for(p=head1; p->next != NULL; p=p->next)
{
    for(pp = head1,temp = head1->next; temp != NULL; pp=pp->next,temp=temp->next)
    {
        if(pp->val > temp->val)
        {
            ch = pp->val;
            pp->val = temp->val;
            temp->val = ch;
        }
    }
}



return head1;

}

全部评论

相关推荐

夏日狂想曲:连体婴是这样的,不过国内还有上四休三的公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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