-2

Can you please explain me the meaning of following lines in below linked list code and if you know any alternate method to these lines then please share it as well.

  1. ListNode *l3=NULL;
  2. ListNode **node=&l3;
  3. (*node)=new ListNode(sum%10);
  4. node=&((*node)->next);

I have also marked them in the code as query 1, query2, query3 and query 4 for your convinience

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


class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int sum=0;
        ListNode *l3=NULL; //----------------------------------Query 1
        ListNode **node=&l3;//---------------------------------Query 2
        while(l1!=NULL||l2!=NULL||sum>0)
        {
            if(l1!=NULL)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2!=NULL)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            (*node)=new ListNode(sum%10);//--------------------Query 3
            sum/=10;
            node=&((*node)->next);//---------------------------Query 4
        }        
        return l3;
    }
};
  • 1
    What exactly about these statements do you need help understanding? Have you have a [decent C++ book](https://stackoverflow.com/questions/388242/) to reference for things like this? – Remy Lebeau Mar 10 '22 at 19:39
  • @Abhishek Kumar What is unclear in this declaration ListNode *l3=NULL;? – Vlad from Moscow Mar 10 '22 at 19:49
  • Recommendation: Don't be THAT guy. Use descriptive variable names. If you chose identifiers that convey meaning, the code will usually explain itself. – user4581301 Mar 11 '22 at 00:39

1 Answers1

2

ListNode *l3=NULL;

Declares a variable named l3 of type ListNode*, and initializes it to NULL.

ListNode **node=&l3;

Declares a variable named node of type ListNode**, and initializes it to point at the memory address of l3.

(*node)=new ListNode(sum%10);

Dereferences node to access the current ListNode* it is pointing at, and then sets that pointer to point at a new ListNode object constructed with sum%10 for its data.

node=&((*node)->next);

Sets node to point at the memory address of the new ListNode's next member.

This double-pointer trick is commonly used to keep track of the next ListNode* pointer that needs to be updated on each loop iteration, to avoid having to use an if statement and an extra variable, eg:

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ...
    ListNode *l3 = NULL, *last = NULL, *node;
    while (...)
    {
        ...
        node = new ListNode(...);
        ...
        if (l3 == NULL)
            l3 = node;
        else
            last->next = node;
        last = node;
    }        
    return l3;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770