Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Computer Science

David Toops
David Toops
18,684 Points

Linked List Merge Sort, why would do you continue to traverse the left or right when the other list is empty?

While Loop from Merge Sort:

while left_head or right_head:
    # If the head node of left is None, we're past the tail
    # Add the node from right to merged linked list
    if left_head is None:
      current.next_node = right_head
      # Call next on right to set loop condition to False
      right_head = right_head.next_node

The left_head is none, so the left list is empty and what you want to do is add the right list to the merged list. Wouldn't this be computationally faster...

  if left_head is None:
    #  Add right list to merge list
    current.next_node = right_head
    #  Remove reference to right list since it has been added to merged
    right_head = None

@pasanpr