LeetCode Merge Two Sorted Lists problem

I’m following this guy’s solution and at 2:48 the way he accounts for the edge cases doesn’t seem complete to me?

There are surely cases where when you’ve traversed until the end for one list, there could be still more than one nodes left in the other. For example, for the input l1 = [1, 2, 3] and l2 = [4, 5, 6], the entirety of l2 is remaining after traversing l1. Wouldn’t the if statements he’s added to the end only add one node to the output, rather than the rest leftover (e.g for the example inputs I’ve provided, only 4 from l2 gets added to the merged lists) as the if statements only execute once?

Thank you!

Hi I still need help

yes, that’s correct.

However it does not breaks the code, because if statements still inside the while loop

look at the condition of the while loop - sorting will stop ONLY IF both lists are traversed

edge cases are triggered only if one of the lists traversed completely, but not both of them

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.