跳转至

23. Merge k Sorted Lists

Leetcode Linked List

题目

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

中文题目

k个有序的链表拼接成一个有序的链表。

思路

为了避免分类讨论,添加一个假的头节点。现在只需要两个指针分别指向原来的两个链表,将其中比较小的节点添加到新的链表中。传入的参数l1和l2正好可以当作遍历两个链表的指针。

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

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        temp = ListNode(-1)
        head = temp
        while l1 and l2:
            if l1.val < l2.val:
                temp.next = l1
                l1 = l1.next
            else:
                temp.next = l2
                l2 = l2.next
            temp = temp.next

        if l1:
            temp.next = l1

        if l2:
            temp.next = l2


        return head.next

Your runtime beats 100.00 % of python3 submissions.