Skip to content

Bug Report for merge-k-sorted-linked-lists #5713

@pgiardiniere

Description

@pgiardiniere

Bug Report for https://neetcode.io/problems/merge-k-sorted-linked-lists

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Seems like type checking is broken for this problem. When I submit an answer which previously was accepted (and matches code found in the solutions tab) I now receive an error when submitting an empty list [] or None for appropriate test case inputs 2 and 3. Code and Error found below:

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

class Solution:    
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        if not lists:
            return []

        for i in range(1, len(lists)):
            lists[i] = self.mergeTwoLists(lists[i - 1], lists[i])
        
        return lists[-1]
    
    def mergeTwoLists(self, l1, l2):
        dummy = merged = ListNode()

        while l1 and l2:
            if l1.val < l2.val:
                merged.next = l1
                l1 = l1.next
            else:
                merged.next = l2
                l2 = l2.next
            merged = merged.next
        
        if l1:
            merged.next = l1
        if l2:
            merged.next = l2
        
        return dummy.next

Error:

Traceback (most recent call last):
  File "/box/main.py", line 111, in <module>
    raise TypeError(f"Your output was {head}, but the expected return type is Optional[ListNode]")
TypeError: Your output was [], but the expected return type is Optional[ListNode]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions