© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
17 replies
Wrenpo

Studying singly linked lists and need help understanding pointer functionality

I would love some help understanding something in the following leetcode problem Merge Two Sorted Lists:

void Main()
{
    var list1 = new ListNode(1, new ListNode(2, new ListNode(4, null)));
    var list2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));
    
    MergeTwoLists(list1, list2).Dump();    // expected output: [ 1, 1, 2, 3, 4 ]
}

public class ListNode
{
    public int val;
    public ListNode next;
    
    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}

public ListNode MergeTwoLists(ListNode list1, ListNode list2)
{
    if (list1 == null) return list2;
    if (list2 == null) return list1;
    
    ListNode dummy = new();
    ListNode current = dummy;
    
    while (list1 != null && list2 != null)
    {
        int value = 0;
        
        if (list1.val <= list2.val)
        {
            value = list1.val;
            list1 = list1.next;
        }
        else
        {
            value = list2.val;
            list2 = list2.next;
        }
        
        ListNode temp = new(value);
        current.next = temp;      // This modifies dummy...
        current = current.next;   // This does not...
    }

    if (list1 == null) current.next = list2;
    else if (list2 == null) current.next = list1;
    
    return dummy.next;
}
void Main()
{
    var list1 = new ListNode(1, new ListNode(2, new ListNode(4, null)));
    var list2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));
    
    MergeTwoLists(list1, list2).Dump();    // expected output: [ 1, 1, 2, 3, 4 ]
}

public class ListNode
{
    public int val;
    public ListNode next;
    
    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}

public ListNode MergeTwoLists(ListNode list1, ListNode list2)
{
    if (list1 == null) return list2;
    if (list2 == null) return list1;
    
    ListNode dummy = new();
    ListNode current = dummy;
    
    while (list1 != null && list2 != null)
    {
        int value = 0;
        
        if (list1.val <= list2.val)
        {
            value = list1.val;
            list1 = list1.next;
        }
        else
        {
            value = list2.val;
            list2 = list2.next;
        }
        
        ListNode temp = new(value);
        current.next = temp;      // This modifies dummy...
        current = current.next;   // This does not...
    }

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


When debugging the solution, I can see that during the while loop that setting the pointer
current.next = temp
current.next = temp
modifies
dummy.next
dummy.next
to equal the
temp
temp
ListNode. However, setting the pointer
current = current.next
current = current.next
does not modify dummy. Why is that?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Lists and understanding the errors
C#CC# / help
4w ago
❔ Need Help Understanding Casting
C#CC# / help
3y ago
✅ Need help understanding parent and child views
C#CC# / help
2y ago