简体   繁体   中英

Confusion about linked list traversal and debugging

I was practicing linked lists and I'm unsure about what is going wrong here.

In the first implementation I made a ListNode* result and gave it any value and when instead of returning the dummyhead, I returned dummyhead->next ( resultHead->next ).

This works as expected.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

Code:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* result = new ListNode(0);
    ListNode* resultHead = result;
    int carry(0);
    while(l1 || l2 || carry) {
        int val1 = l1 ? l1->val : 0;
        int val2 = l2 ? l2-> val : 0;

        int sum(val1 + val2 + carry);
        carry = sum/10;

        ListNode* l3 = new ListNode(sum % 10);
        result->next = l3;
        result = result->next;

        if(l1) l1 = l1->next;
        if(l2) l2 = l2->next;
    }
    return resultHead->next;
}

Now I thought maybe I could avoid this if I initially gave it a nullptr have a check for null and assign it the new node, and continue as before, but in this case, following the debugger, the list starts losing elements, when I do result = result->next .

Code:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* result = NULL; // also tried nullptr
    ListNode* resultHead = result;
    int carry(0);
    while(l1 || l2 || carry) {
        int val1 = l1 ? l1->val : 0;
        int val2 = l2 ? l2-> val : 0;

        int sum(val1 + val2 + carry);
        carry = sum/10;

        ListNode* l3 = new ListNode(sum % 10);
        if(!result) result = l3;
        else {
            result->next = l3;
            result = result->next;
        }

        if(l1) l1 = l1->next;
        if(l2) l2 = l2->next;
    }
    return resultHead;
}

One issue I see is that the resultHead is not getting updated, which is something to deal with I guess (update the condition in the if block)

, but why does the list not get made properly doing it this way? What is the reason behind it?

edit:

Input lists
l1: [2,4,3]
l2: [5,6,4]
It assigns 7 to "result" in first pass
In second pass the list becomes [7,0] at the `result->next = l3` step in the else block but at the result = result->next the list becomes [0];

The issue was only not updating resultHead (which I knew how to fix and needed fixing, but I thought I should check if the list is being made fine otherwise) while following the linkedlist it only points to one element at a time, and that threw me off (first time debugging a linked list), the value at that point being 0 coincidentally led me to incorrectly believe the list was being null, but that was not the case.

Edit: My issue was also not being familiar with debugging linked lists, it's supposed to show only elements ahead as we move forward.

This is basically what your first code does:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* result = new ListNode(0);
    ListNode* resultHead = result;
    while(l1 || l2 ) {

        ListNode* l3 = new ListNode(0);
        result->next = l3;
        result = result->next;

        if(l1) l1 = l1->next;
        if(l2) l2 = l2->next;
    }
    return resultHead->next;
}

At the beginning there is a single node say n0 ( result and resultHead pointing to it). And at each turn in the loop you are adding a new node behind result, thus n0->n1, then n0->n1->n2, etc. And the end you are returning n1, thus the list n1->n2->...

In the second case:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* result = NULL; // also tried nullptr
    ListNode* resultHead = result;
    while(l1 || l2 || carry) {

        ListNode* l3 = new ListNode(0);
        if(!result) result = l3;
        else {
            result->next = l3;
            result = result->next;
        }

        if(l1) l1 = l1->next;
        if(l2) l2 = l2->next;
    }
    return resultHead;
}

At the beginning you have result and resultHead equals to null. Thus whatever is happening in the loop (which does nothing with resultHead) you are just returning resultHead which is null. You can just modify the positive case of the internal if:

if(!result) {
    result = l3;
    resultHead = result;
}

thus at the end, resultHead will equals to the very first created node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM