leetcode : 23. Merge k Sorted Lists

题目:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

已经定义好的 ListNode 类

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

思路:

方法一、遍历每个链,记录每个数字有几个,然后将这些数字组成一个新链

方法二、比较各个链当前的数字

方法三、所有的链两个合并,最终合成一条链

 

代码:

方法一:

import collections

class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        nums = collections.defaultdict(lambda: 0)
        for node in lists:
            while node:
                nums[node.val] += 1
                node = node.next
        
        node = ListNode(0)
        start = node
        keys = sorted(nums.keys())
        for key in keys:
            for i in range(nums[key]):
                node_next = ListNode(key)
                node.next = node_next
                node = node_next
        return start.next

方法二:

    def get_min_node(self,nodes):
    # 选取value最小的node
        val_min = 2**31
        for node in nodes:
            if node.val < val_min:
                val_min = node.val
                mini_node = node
        return mini_node
    
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
      
        start = ListNode(0)
        temp_n = start
        nodes = []
        for node in lists:
            if node:
                nodes.append(node)
        while len(nodes)>0:
            node = self.get_min_node(nodes)
            temp_n.next = node
            temp_n = node
            nodes.remove(node)
            if node.next:
                nodes.append(node.next)
        return start.next

方法三: 两两合并

    def merge2Lists(self,list1:ListNode,list2:ListNode):
        if list1 and list2:
            if list1.val < list2.val:
                current = list1
                other = list2
            else:
                current = list2
                other = list1
            start = current
            prev = current

            while current and other:
                if current.val <= other.val:
                    prev = current
                    current = current.next
                else:
                    prev.next = other
                    prev = other
                    other = current
                    current = prev.next
            if current:
                prev.next = current
            elif other:
                prev.next = other
            return start
        elif list1:
            return list1
        elif list2:
            return list2
        else:
            return None
            
    def mergeKLists(self, lists) -> ListNode:
        while len(lists)>=2:
            list1 = lists.pop()
            list2 = lists.pop()
            node_list = self.merge2Lists(list1,list2)
            lists.insert(0,node_list)
        if lists: 
            return lists[0]
发布了45 篇原创文章 · 获赞 1 · 访问量 3377

猜你喜欢

转载自blog.csdn.net/qq_22498427/article/details/104466808