'Why does the head pointer lose track of the linkedlist when overwriting the current head node

Comparing:

public static ListNode mergeTwoLists(ListNode list1, ListNode list2)
    {
        ListNode dummy = new ListNode(-1);
        ListNode head = dummy;

        while (list1 != null && list2 != null)
        {
            if (list1.val < list2.val)
            {
                dummy.next = list1;
                list1 = list1.next;
            }
            else
            {
                dummy.next = list2;
                list2 = list2.next;
            }

            dummy = dummy.next;
        }
        if (list1 == null)
        {
            dummy.next = list2;
        }
        else if (list2 == null)
        {
            dummy.next = list1;
        }
        return head.next;
    }

To:

 public static ListNode mergeTwoLists(ListNode list1, ListNode list2)
        {
            ListNode dummy = new ListNode(-1);
            ListNode head = dummy;

        while (list1 != null && list2 != null)
        {
            if (list1.val < list2.val)
            {
                dummy = list1;
                list1 = list1.next;
            }
            else
            {
                dummy = list2;
                list2 = list2.next;
            }

            dummy = dummy.next;
        }
        if (list1 == null)
        {
            dummy.next = list2;
        }
        else if (list2 == null)
        {
            dummy.next = list1;
        }
        return head.next;
    }

What is the difference in respect to how head points to the dummy ListNode.

My understanding was instead of using dummy.next = list1 and just having dummy = list1, we would essentially be rewriting the current value of dummy. Then when we get to:

dummy = dummy.next;

Dummy will equal null. So when we get to the next iteration of the loop we will resassign null to the whatever node is in list1. So why is head not able to keep track of dummy when we do this? Is it because we are assigning the first node of dummy to a different list?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source