力扣题:链表的合并-10.17

力扣题-10.17

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:21. 合并两个有序链表

解题思想:合并链表就行

力扣题:链表的合并-10.17_第1张图片

# 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;
    }
};

力扣题2:23. 合并K个升序链表

解题思想:遍历列表,采取第一问的方法即可

力扣题:链表的合并-10.17_第2张图片

# 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;
        }
    }
};

你可能感兴趣的:(leetcode,链表)