Share python solution


  • -3
    A

    This solution probably isn't what's expected for this problem.
    But its' fast :D

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param {ListNode[]} lists
        # @return {ListNode}
        
        def mergeKLists(self, lists):
            lists = [lis for lis in lists if lis is not None]
            if not lists:
                return None
           
            vals = []
            for head in lists:
                while head is not None:
                    vals.append(head.val)
                    head = head.next
            vals.sort()
            
            head = ListNode(vals[0])
            itr = head
            for i in vals[1:]:
                itr.next = ListNode(i)
                itr = itr.next
            
            return head

  • 0
    H

    This solution isn't necessarily a bad solution. It's still O(n log n) anyways.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.