2. Add two numbers

Keywords: P4

2. Add two numbers

Original topic connection

1. Title Description

Two non empty linked lists are given to represent two non negative integers. Their respective digits are stored in reverse order, and each node can only store one digit.
If we add the two numbers together, we will return a new linked list to represent their sum.
You can assume that neither of these numbers starts with 0 except for the number 0.
Example:
Input: (2 - > 4 - > 3) + (5 - > 6 - > 4)
Output: 7 - > 0 - > 8
Reason: 342 + 465 = 807

2. The code is as follows

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *p1 = l1;
    struct ListNode *p2 = l2;
    
    struct ListNode *l3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode *p3 = l3;
    
    int carry = 0;
    int sum = 0;
    
    while( p1 != NULL&&p2 != NULL)
    {//The previous problem was that I assigned sum to the first node of the l3 chain, resulting in an empty node in the list I finally returned. Now I will create a list with an empty header node, but the starting node i returned is the next node of the header node
        sum = p1->val + p2->val + carry;
        if(sum>9)//First condition
        {
           sum%=10;//Ah ah ah!!!
           carry = 1;
        }
        else
        {
            carry = 0;
        }
        struct ListNode *p4 = (struct ListNode*)malloc(sizeof(struct ListNode));
        p4->val = sum;
        p4->next = NULL;
        p3->next = p4;
        p3 = p4;
        p1 = p1->next;
        p2 = p2->next;
    }
    while(p1!=NULL)//p1 longer
    {
        sum = p1->val + carry;
        if(sum>9)//First condition
        {
           sum%=10;//Ah ah ah!!!
           carry = 1;
        }
        else
        {
            carry = 0;
        }
        struct ListNode *p5 = (struct ListNode*)malloc(sizeof(struct ListNode));
        p5->val = sum;
        p5->next = NULL;
        p3->next = p5;
        p3 = p5;
        p1 = p1->next;
    }
    while(p2!=NULL)//p2 longer
    {
        sum = p2->val + carry;
        if(sum>9)//First condition
        {
           sum%=10;//Ah ah ah!!!
           carry = 1;
        }
        else
        {
            carry = 0;
        }
        struct ListNode *p6 = (struct ListNode*)malloc(sizeof(struct ListNode));
        p6->val = sum;
        p6->next = NULL;
        p3->next = p6;
        p3 = p6;
        p2 = p2->next;
    }
    if(carry == 1)//The value combination of two linked lists is completed, but the maximum value bit is carried
    {//I made an error before. I wrote if as while, which caused time-out
        sum = carry;
        struct ListNode *p7 = (struct ListNode*)malloc(sizeof(struct ListNode));
        p7->val = sum;
        p7->next = NULL;
        p3->next = p7;
    }
    
    return l3->next;//The header node of l3 is discarded (because it is empty, we only generate it for indexing purpose
}
 1.Key point: we built it from the beginning L3 Linked list (length:1),And secondary index p3,Calculate on real edge
 //In the process of node generation, the most important thing is that we assign the value to the node generated in the loop instead of
 //Starting from the head node of l3, because our list is a one-way list, if we start from the head of l3
 //Assignment. Finally, we need to go through a malloc generation node in the loop, but there is no value to assign to it
 //As a result, there is an empty node at the end of the final generated list, and our one-way list can only'after'If we can't move forward, we can't find a good way to deal with this last node, so we choose
 //Value fromwhileThe node generated in the loop is assigned, so when we return to the linked list, we only need to l3->next open
 //Just go back.
```c

Posted by ainoy31 on Tue, 12 Nov 2019 09:37:28 -0800