题解 | #合并两个排序的链表#
合并两个排序的链表
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;
}