addTwoNumber

Posted buaawang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了addTwoNumber相关的知识,希望对你有一定的参考价值。

leetcode--addTwoNumber

(```)

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
struct ListNode dummy;
dummy.next = NULL;
struct ListNode p, node;
struct ListNode p1, p2;
p = &dummy; p1 =l1; p2 = l2;

int cin=0, sum=0;
while(p1 || p2)
    sum = cin;
    if(p1)
        sum += p1->val;
        p1 = p1->next;
    
    if(p2)
        sum += p2->val;
        p2 = p2->next;
    
    
    node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->val = sum % 10;
    node->next = NULL;
    p->next = node;
    p= node;
    
    cin = sum / 10;
    

if(cin)
    node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->val = cin;
    node->next = NULL;
    p->next = node;
    p = node;

return dummy.next;

(```)

以上是关于addTwoNumber的主要内容,如果未能解决你的问题,请参考以下文章