非常easy问题。
唯一的地方可以具有更具挑战是确保不会引入额外的空间。查找开始值最小的名单列表的新掌门人,头从列表中删除。其他操作应该没有问题。
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->val>l2->val)
swap(l1, l2);
ListNode *head = l1, *pNode = l1;
l1 = l1->next;
head->next = NULL;
while(l1&&l2){
if(l1->val <= l2->val){
pNode->next = l1;
pNode = l1;
l1 = l1->next;
}else{
pNode->next = l2;
pNode = l2;
l2 = l2->next;
}
pNode->next = NULL;
}
if(l1)
pNode->next = l1;
if(l2)
pNode->next = l2;
return head;
}
};版权声明:本文博客原创文章,博客,未经同意,不得转载。