A C language error in question "Add Two Numbers"


  • 0
    C

    I get an error result [0,0,8] when I run my code in leetcode,but I can get a correct result[7,0,8] when I run code in codeblock.I don't know why,could help me?

     /**
         * Definition for singly-linked list.
         * struct ListNode {
         *     int val;
         *     struct ListNode *next;
         * };
         */
        struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
            int sum1,sum2=0;
            struct ListNode *head,*p,*q;
            head=(struct ListNode*)malloc(sizeof(struct ListNode));
            head->next=NULL;
            p=head;
            while(l1->next!=NULL && l2->next!=NULL)
            {
                q=(struct ListNode*)malloc(sizeof(struct ListNode));
                sum1=l1->next->val+l2->next->val+sum2;
                sum2=sum1/10;
                q->val=sum1%10;
                q->next=NULL;
                p->next=q;
                p=q;
                l1=l1->next;
                l2=l2->next;
            }
            return head;
        }

Log in to reply
 

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