题解 | #合并k个已排序的链表#
合并k个已排序的链表
https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeKLists(self , lists: List[ListNode]) -> ListNode: # write code here start = ListNode(-1) for head in lists: start = self.merge(start.next,head) return start.next def merge(self,head1,head2): start = ListNode(-1) cur = start while head1 and head2: if head1.val<=head2.val: cur.next = head1 head1 = head1.next else: cur.next = head2 head2 = head2.next cur = cur.next cur.next = head1 if head1 else head2 return start