<s> import heapq <EOL> class ListNode : <EOL> def __init__ ( self , x ) : <EOL> self . val = x <EOL> self . next = None <EOL> def __repr__ ( self ) : <EOL> if self : <EOL> return "<STR_LIT>" . format ( self . val , repr ( self . next ) ) <EOL> class Solution : <EOL> def mergeKLists ( self , lists ) : <EOL> dummy = ListNode ( <NUM_LIT:0> ) <EOL> current = dummy <EOL> heap = [ ] <EOL> for sorted_list in lists : <EOL> if sorted_list : <EOL> heapq . heappush ( heap , ( sorted_list . val , sorted_list ) ) <EOL> while heap : <EOL> smallest = heapq . heappop ( heap ) [ <NUM_LIT:1> ] <EOL> current . next = smallest <EOL> current = current . next <EOL> if smallest . next : <EOL> heapq . heappush ( heap , ( smallest . next . val , smallest . next ) ) <EOL> return dummy . next <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> list1 = ListNode ( <NUM_LIT:1> ) <EOL> list1 . next = ListNode ( <NUM_LIT:3> ) <EOL> list2 = ListNode ( <NUM_LIT:2> ) <EOL> list2 . next = ListNode ( <NUM_LIT:4> ) <EOL> print Solution ( ) . mergeKLists ( [ list1 , list2 ] ) </s>
