add two numbers

我的代码

代码一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void add(int *value,int *step){
    if(*value>=10){
        *step=*value/10;
        *value%=10;
    }else
    {
        *step=0;
    }

}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int step=0,tmpValue;
    struct ListNode *result=NULL;
    struct ListNode *current=NULL;
    struct ListNode *pre=NULL;
    for(;l1!=NULL&&l2!=NULL;l1=l1->next,l2=l2->next){
        tmpValue=l1->val+l2->val+step;
        add(&tmpValue,&step);
        current=(struct ListNode*)malloc(sizeof(struct ListNode));

        current->val=tmpValue;
        if(!pre&&!result){
            result=current;
            pre=current;
        }else
        {
            pre->next=current;
            pre=current;
        }
    }
    if(l1){
        for(;l1;l1=l1->next){
            current=(struct ListNode*)malloc(sizeof(struct ListNode));
            tmpValue=l1->val+step;
            add(&tmpValue,&step);
            current->val=tmpValue;
            pre->next=current;
            pre=current;
        }
    }
    if(l2){
        for(;l2;l2=l2->next){
            current=(struct ListNode*)malloc(sizeof(struct ListNode));
            tmpValue=l2->val+step;
            add(&tmpValue,&step);
            current->val=tmpValue;
            pre->next=current;
            pre=current;
        }
    }
    if(step>0){
        current=(struct ListNode*)malloc(sizeof(struct ListNode));
        current->val=step;
        pre->next=current;
        pre=current;
    }
    current->next=NULL;
    return result;
}

运行结果

1
2
Runtime: 32 ms
Memory Usage: 17.9 MB