合并已经排序的2条链表(day11) 发表于 2017-02-13 | 题目 合并2条链表1如 1,3,5,7 和 2,4,6,8 -> 1,2,3,4,5,6,7,8 实现1234567891011121314151617181920212223242526272829303132333435363738ListNode * mergeList(ListNode* list1, ListNode* list2){ if(!list1) return list2; if(!list2) return list1; ListNode* head; ListNode* tail = new ListNode(); head = tail; while(list1 || list2) { if(!list2 || list1->value < list2->value) { tail->next = list1; tail=tail->next; list1=list1->next; } else { tail->next=list2; tail=tail->next; list2=list2->next; } } return head->next;}//使用递归ListNode * mergeList2(ListNode* list1, ListNode* list2){ if(!list1) return list2; if(!list2) return list1; ListNode* head; if(list1->value < list2->value){ head = list1; head->next = mergeList2(list1->next, list2); } else{ head = list2; head->next = mergeList2(list1,list2->next); } return head;} 源码传送门 赏 微信打赏 支付宝打赏