0
0
DSA Pythonprogramming~10 mins

Merge Two Sorted Linked Lists in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Merge Two Sorted Linked Lists
Create dummy node
Set current pointer to dummy
Compare heads of both lists
Smaller node
Move current and chosen list pointer
Repeat until one list is empty
Attach remaining nodes of non-empty list
Return dummy.next as merged list head
We create a dummy node to start the merged list, then repeatedly pick the smaller head node from the two lists, attach it to the merged list, and move pointers until one list is empty. Finally, we attach the leftover nodes.
Execution Sample
DSA Python
def mergeTwoLists(l1, l2):
    dummy = ListNode(0)
    current = dummy
    while l1 and l2:
        if l1.val < l2.val:
            current.next = l1
            l1 = l1.next
        else:
            current.next = l2
            l2 = l2.next
        current = current.next
    current.next = l1 or l2
    return dummy.next
This code merges two sorted linked lists by comparing their nodes and building a new sorted list.
Execution Table
StepOperationl1 Nodel2 NodeCurrent PointerPointer ChangesMerged List Visual
1Create dummy node and set current1 -> 3 -> 5 -> null2 -> 4 -> 6 -> nulldummy(0)current = dummy0(dummy) -> null
2Compare l1.val(1) < l2.val(2)1 -> 3 -> 5 -> null2 -> 4 -> 6 -> nulldummy(0)current.next = l1(1)0(dummy) -> 1 -> null
3Move l1 to next (3), current to next (1)3 -> 5 -> null2 -> 4 -> 6 -> null1l1 = 3, current = 10(dummy) -> 1 -> null
4Compare l1.val(3) < l2.val(2)3 -> 5 -> null2 -> 4 -> 6 -> null1current.next = l2(2)0(dummy) -> 1 -> 2 -> null
5Move l2 to next (4), current to next (2)3 -> 5 -> null4 -> 6 -> null2l2 = 4, current = 20(dummy) -> 1 -> 2 -> null
6Compare l1.val(3) < l2.val(4)3 -> 5 -> null4 -> 6 -> null2current.next = l1(3)0(dummy) -> 1 -> 2 -> 3 -> null
7Move l1 to next (5), current to next (3)5 -> null4 -> 6 -> null3l1 = 5, current = 30(dummy) -> 1 -> 2 -> 3 -> null
8Compare l1.val(5) < l2.val(4)5 -> null4 -> 6 -> null3current.next = l2(4)0(dummy) -> 1 -> 2 -> 3 -> 4 -> null
9Move l2 to next (6), current to next (4)5 -> null6 -> null4l2 = 6, current = 40(dummy) -> 1 -> 2 -> 3 -> 4 -> null
10Compare l1.val(5) < l2.val(6)5 -> null6 -> null4current.next = l1(5)0(dummy) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
11Move l1 to next (null), current to next (5)null6 -> null5l1 = null, current = 50(dummy) -> 1 -> 2 -> 3 -> 4 -> 5 -> null
12l1 is empty, attach remaining l2null6 -> null5current.next = l2(6)0(dummy) -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
13Return dummy.next as merged list headnull6 -> null5return dummy.next1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
💡 Loop ends when l1 is empty at step 11, remaining l2 nodes attached at step 12
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11Final
l11 -> 3 -> 5 -> null3 -> 5 -> null3 -> 5 -> null5 -> null5 -> nullnullnullnullnullnullnullnullnull
l22 -> 4 -> 6 -> null2 -> 4 -> 6 -> null4 -> 6 -> null4 -> 6 -> null6 -> null6 -> null6 -> null6 -> null6 -> null6 -> null6 -> null6 -> null6 -> null
currentdummy(0)123455555555
Key Moments - 3 Insights
Why do we use a dummy node instead of starting directly with l1 or l2?
The dummy node simplifies the code by providing a fixed starting point for the merged list. Without it, we would need extra checks to set the head of the merged list. See execution_table step 1 where dummy is created and current points to it.
Why do we attach the remaining nodes after one list is empty?
Because both lists are sorted, the remaining nodes in the non-empty list are already in order and can be attached directly. This happens at step 12 in the execution_table.
Why do we move the current pointer after attaching a node?
Moving current ensures that the next attached node is linked correctly after the last node in the merged list. This is shown in steps 3, 5, 7, 9, and 11 where current moves forward.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, which node is attached to the merged list?
ANode with value 4 from l2
BNode with value 3 from l1
CNode with value 2 from l2
DNode with value 5 from l1
💡 Hint
Check the 'Pointer Changes' and 'Merged List Visual' columns at step 6 in execution_table
At which step does l1 become empty?
AStep 11
BStep 10
CStep 12
DStep 13
💡 Hint
Look at the 'l1 Node' column in execution_table and variable_tracker to see when it becomes null
If l1 started with values all greater than l2, how would the merged list start?
AWith nodes from l1 first
BAlternating nodes from l1 and l2
CWith nodes from l2 first
DEmpty list
💡 Hint
Since we always attach the smaller node first, check the comparison logic in concept_flow and execution_table
Concept Snapshot
Merge Two Sorted Linked Lists:
- Use a dummy node to start merged list
- Compare heads of both lists
- Attach smaller node to merged list
- Move pointers forward
- Repeat until one list is empty
- Attach remaining nodes
- Return dummy.next as head
Full Transcript
To merge two sorted linked lists, we start by creating a dummy node to simplify list construction. We use a current pointer starting at dummy. We compare the values at the heads of both lists and attach the smaller node to current.next. Then we move current and the chosen list's pointer forward. We repeat this until one list is empty. Finally, we attach the remaining nodes of the non-empty list to current.next. We return dummy.next as the head of the merged sorted list. This approach ensures the merged list is sorted without extra sorting steps.