leetcode题目2. 两数相加

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
int jinwei,b1,b2,a1,a2;
        b1=1;
        b2=1;
        jinwei=0;
        struct ListNode* p;
        struct ListNode* p1;
        struct ListNode* p2;
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->next=NULL;
        p1=p;
        while(1)
        {
            if(l1==NULL)
            {b1=0;
             a1=0;
            }
            else
                a1=l1->val;
            if(l2==NULL)
            {a2=0;
                b2=0;
            
             }
            else
                a2=l2->val;
                
            if(b1+b2==0)
            {
                if(jinwei!=0)
                {
                    p2->next->val=jinwei;
                    break;
                }
                else
                {p2->next=NULL;
                break;}
            }
            p->val=a1+a2+jinwei;
            jinwei=p->val/10;
            p->val=p->val%10;
            if(b1==1)
                l1=l1->next;
            if(b2==1)
                l2=l2->next;
            struct ListNode* p0;
            p0=(struct ListNode*)malloc(sizeof(struct ListNode));
            p0->next=NULL;
            p2=p;
            p->next=p0;
            p=p0;
        }
        return p1;
}


你可能感兴趣的:(leetcode,两数相加,编程日记)