class Solution {
public:
/**
* @param ListNode l1 is the head of the linked list
* @param ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// write your code here
ListNode* l3 = new ListNode(0);
ListNode* l4=l3;
ListNode* l5=l4;
while ( l1 != NULL&&l2 != NULL ){
if ( l1->val < l2->val ){
l3->next = l1;
l1 = l1->next;
l3 = l3->next;
}
else {
l3->next = l2;
l2 = l2->next;
l3 = l3->next;
}
}
if ( l1 == NULL ){
l3->next = l2;
}else
l3->next = l1;
delete l5;
return l4->next;
}
};
LintCode-剑指Offer-(165)合并两个排序链表
最新推荐文章于 2019-02-25 10:05:44 发布