C solution using double pointer


  • 1
    D
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
    {
        if (l1 == NULL)
            return l2;
        if (l2 == NULL)
            return l1;
        struct ListNode *h = NULL;
        struct ListNode **c = &h;
        int v = 0;
        while (l1 != NULL || l2 != NULL || v) {
            if (l1 != NULL) {
                v += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL) {
                v += l2->val;
                l2 = l2->next;
            }
    	struct ListNode *p = (struct ListNode *) malloc(sizeof(struct ListNode));
    	p->next = NULL;
            p->val = v % 10;
    	*c = p;
            c = &p->next;
            v /= 10;
        }
        return h;
    }
    
    

  • 0
    F

    I'm really new one, what is the meaning of **c=h?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.