题目链接
【题目】
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
【分析】
做法(1):可以在new_list前先加一个无关的节点然后返回new_list->next
做法(2):正常做法比较,合并
做法(3):递归
做法(4):事先假定(最高效)
最优runtime:
C++:
正常做法:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
ListNode* new_list = NULL;
if(l1->val < l2->val)
{
new_list = l1;
l1 = l1->next;
}
else
{
new_list = l2;
l2 = l2->next;
}
ListNode * pre = new_list;
while( l1 != NULL && l2 != NULL ){
if( l1->val < l2->val ){
pre->next = l1;
l1 = l1->next;
}
else{
pre->next = l2;
l2 = l2->next;
}
pre = pre->next;
}
if( l1 ) pre->next = l1;
else pre->next = l2;
return new_list;
}
};
增加一个无关节点:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1 && !l2) return NULL;
ListNode * new_list = new ListNode(0);
ListNode * pre = new_list;
while( l1 != NULL && l2 != NULL ){
if( l1->val < l2->val ){
pre->next = l1;
l1 = l1->next;
}
else{
pre->next = l2;
l2 = l2->next;
}
pre = pre->next;
}
if( l1 ) pre->next = l1;
else pre->next = l2;
return new_list->next;
}
};
递归:
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l2->next, l1);
return l2;
}
}
};
事先假定:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode * new_list = new ListNode(0);
new_list->next = l1;
ListNode * prev = new_list;
ListNode * headof1 = l1;
ListNode * headof2 = l2;
while( headof2 != NULL ){
if( headof1 == NULL ){
prev->next = headof2;
break;
}
if( headof1->val > headof2->val ){
ListNode * temp = headof2;
headof2 = headof2->next;
prev->next = temp;
temp->next = headof1;
prev = prev->next;
}
else{
prev = headof1;
headof1 = headof1->next;
}
}
return new_list->next;
}
};
python:
递归:
def mergeTwoLists(self, l1, l2):
if not l1:
return l2
elif not l2:
return l1
else:
if l1.val <= l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
非递归:
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None and l2 is None:
return None
new_list = ListNode(0)
pre = new_list
while l1 is not None and l2 is not None:
if l1.val < l2.val:
pre.next = l1
l1 = l1.next
else:
pre.next = l2
l2 = l2.next
pre = pre.next
if l1 is not None:
pre.next = l1
else:
pre.next = l2
return new_list.next
事先假定:
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
new_list = ListNode(0)
new_list.next = l1
prev, head_of_1, head_of_2 = new_list, l1, l2
while head_of_2:
if not head_of_1:
prev.next = head_of_2
break
if head_of_1.val > head_of_2.val:
temp = head_of_2
head_of_2 = head_of_2.next
prev.next = temp
temp.next = head_of_1
prev = prev.next
else:
head_of_1, prev = head_of_1.next, head_of_1
return new_list.next