educative.io

Educative

Error in the merge_sorted method

Hello,
if the first linked list is empty, all the nodes from the second linked list should be added to the first one. But the first linked list remains empty.
To solve this problem you should change the following code of block:

  if not p:
      return q
  if not q:
      return p

to:

    if not p:
        self.head = q
        return self.head
    if not q:
        return self.head

Best regards
Miguel


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi Miguel!
Yeah there’s an issue in this case where the first linked list is empty.
Thank you for your great suggestion. We’ll update this lesson soon.