python写算法题:leetcode: 23. Merge k Sorted Lists

https://leetcode.com/problems/merge-k-sorted-lists/#/description

class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        if len(lists)<1: return []
        
        head=ListNode(0)
        head.next=lists[0]
        for lst in lists[1:]:
            node=head
            item=lst
            while item and node.next:
                if node.next.val > item.val:
                    nodebak = node.next
                    node.next = ListNode(item.val)
                    node = node.next
                    node.next = nodebak
                    item = item.next
                else:
                    node = node.next
            if item:
                node.next=item
        return head.next


你可能感兴趣的:(python,算法)