[力扣刷题攻略] Re:从零开始的力扣刷题生活
解题思想:合并链表就行
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
node1 = list1
node2 = list2
head = ListNode(0)
temp = head
while node1!=None and node2!=None:
if node1.val<=node2.val:
head.next = node1
node1 = node1.next
else:
head.next = node2
node2 = node2.next
head = head.next
if node1!=None:
head.next = node1
if node2!=None:
head.next = node2
return temp.next
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* node1 = list1;
ListNode* node2 = list2;
ListNode* head = new ListNode(0);
ListNode* temp = head;
while(node1!=nullptr && node2!=nullptr){
if(node1->val<=node2->val){
head->next = node1;
node1 = node1->next;
}
else{
head->next = node2;
node2 = node2->next;
}
head = head->next;
}
if(node1!=nullptr){
head->next = node1;
}
if(node2!=nullptr){
head->next = node2;
}
return temp->next;
}
};
解题思想:遍历列表,采取第一问的方法即可
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
if len(lists)==0:
return None
elif len(lists)==1:
return lists[0]
else:
list1 = lists[0]
for i in range(1,len(lists)):
head = ListNode(0)
temp = head
list2 = lists[i]
node1 = list1
node2 = list2
while node1!=None and node2!=None:
if node1.val<=node2.val:
head.next = node1
node1 = node1.next
else:
head.next = node2
node2 = node2.next
head = head.next
if node1!=None:
head.next = node1
if node2!=None:
head.next = node2
list1 = temp.next
return temp.next
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty())return nullptr;
else if(lists.size()==1)return lists[0];
else{
ListNode* list1 = lists[0];
ListNode* temp = nullptr;
for(int i=1;i<lists.size();i++){
ListNode* head = new ListNode(0);
temp = head;
ListNode* list2 = lists[i];
ListNode* node1 = list1;
ListNode* node2 = list2;
while(node1!=nullptr && node2!=nullptr){
if(node1->val<=node2->val){
head->next = node1;
node1 = node1->next;
}
else{
head->next = node2;
node2 = node2->next;
}
head = head->next;
}
if(node1!=nullptr){
head->next = node1;
}
if(node2!=nullptr){
head->next = node2;
}
list1 = temp->next;
}
return temp->next;
}
}
};