Skip to content

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

Description

@NJiHin

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.
Missing teset case for this problem.
I tried submitted the below solution, which passed all test cases:

# 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]:
        
        def merge2(l1, l2):
            dummy = ListNode()
            node = dummy

            while l1 and l2:
                if l1.val < l2.val:
                    node.next = l1
                    l1 = l1.next
                else:
                    node.next = l2
                    l2 = l2.next
                node = node.next
            if l1:
                node.next = l1
            elif l2:
                node.next = l2
            return dummy.next
        
        if len(lists) < 2:
            return None
        
        while len(lists) != 1:
            l1 = lists.pop()
            l2 = lists.pop()
            lists.append(merge2(l1,l2))
        
        return lists[0]

But running a test case such as [[5]] would cause it to fail.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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