You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Leetcode 23. Merge k Sorted Lists运行时错误求助:返回值类型不符

Leetcode 23. Merge k Sorted Lists运行时错误排查

问题现象

解决Leetcode 23题时,代码输出符合预期,但持续触发运行时错误,错误信息如下:

Runtime Error
TypeError: [] is not valid value for the expected return type ListNode
    raise TypeError(str(ret) + " is not valid value for the expected return type ListNode");
Line 65 in _driver (Solution.py)
    _driver()
Line 71 in <module> (Solution.py)

我的代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        if not lists: return lists 
        if len(lists) == 1: return lists[0]
        # return itself if 'lists' is empty or only has one ListNode

        head = ListNode() 
        dummy_front = head
        dummy_last = ListNode(10**5) 
        # During comparison, if a linkedlist is out of nodes, assign dummy_last to be its last node. The maximum value of a listnode is 10**4, so dummy_last would be the largest.*
        flag = 0

# Use all_values to contain the current listnodes' values of all Linked lists. For example, with lists = [[1,4,5],[1,3,4],[2,6]], all_values = [1, 1, 2]
        all_values = [] 
        for i in range(0, len(lists)):
            all_values.append(lists[i].val)
            
        while True:
            min_value = min(all_values) 
            if min_value == 10**5: break # Finished merging of all linked lists
            min_index = all_values.index(min_value)

            if flag == 0: # To decide if it's the first node of our answer linked list to assign the "head"
                head.next = lists[min_index]
                dummy_front = lists[min_index]
                flag = 1
            else:
                dummy_front.next = lists[min_index]
                dummy_front = dummy_front.next
            all_values.pop(min_index) # Pop the used smallest value
            if not lists[min_index].next: # If the linked list has no other nodes, assign dummy_last to be its last node.
                lists[min_index] = dummy_last
                
            else:
                lists[min_index] = lists[min_index].next
            all_values.insert(min_index, lists[min_index].val) # Insert new value at min_index
        return head.next

测试情况

Leetcode官方示例:

Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6

我的代码输出结果为:

ListNode{val: 1, next: ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}}}}}}

与预期一致,但仍触发运行时错误,求排查修复。

问题原因与修复

核心错误

当输入lists为空列表时,代码返回lists(即空列表[]),但题目要求返回类型是ListNode,空列表不符合类型要求,必须返回None。

修复步骤

  1. 修改空输入的返回值:
if not lists: return None  # 原代码返回lists,改为返回None
if len(lists) == 1: return lists[0]
  1. 额外兼容输入含空链表的场景:
    输入的lists中可能存在None(即某个链表为空),此时遍历lists[i].val会触发AttributeError,需要提前过滤:
# 在开头添加过滤逻辑
lists = [l for l in lists if l is not None]
if not lists: return None
if len(lists) == 1: return lists[0]

修改后即可解决运行时类型错误,同时覆盖更多边界场景。


内容的提问来源于stack exchange,提问作者99Orc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:00:56