Data Structures19 sections · 729 units
Open in Course

Merge Two Sorted Lists Solution

Implement your solution

Implement the merge using the dummy head technique.

Watch for:

  • The dummy head eliminates the "which node becomes the new head?" question
  • After the loop, exactly one list might have remaining nodes
  • You're reusing existing nodes, not creating new ones

Common bugs:

  • Forgetting to advance tail after appending
  • Not handling when one list is initially empty
  • Creating new nodes instead of reusing existing ones

This problem is a building block for merge sort on linked lists. Time: O(n)O(n). Space: O(1)O(1).