LeetCode daily exercise (2) Add Two Numbers

subject

Task analysis

I'll give you two non empty lists. I want you to add them up and return the answer list. The so-called addition is the sum of the number of each corresponding two nodes. It should be noted that the answer cannot exceed 10, only one digit, and if it exceeds 10, carry.

Knowledge points
Linked list of data structure

difficulty
secondary

Solving problems

This question mainly investigates the familiarity with the linked list, mainly the generation of new nodes, the operation training of adding to the original linked list, and other requirements of carry. Starting from the first node of the two linked lists, each new node will generate a new node for the answer. When a node is added, check whether the other node is added. If not, the remaining operation is to add the node of the linked list that is not added to the answer. Also, pay attention to carry. The code is annotated clearly. Please refer to the code for detailed solution ideas.

Code

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *ans;  // The head pointer of the answer
        bool flag = false;  // Mark whether there is carry
        int sum = l1->val + l2->val;
        if (sum >= 10) {  // Because they are all 1-digit numbers, the number of tens should be rounded off, and the number of digits should be reserved, but the carry should be marked
            sum -= 10;
            flag = true;
        }
        ListNode *temp = new ListNode(sum);  // Generate a new node
        ans = temp;  // Assign the node to the header pointer
        while (l1->next != NULL && l2->next != NULL) {  // If both lists are not empty
            l1 = l1->next;
            l2 = l2->next;
            int sum = l1->val + l2->val;
            if (flag) {  // If there is carry before
                sum++;   // Add 1 of carry to current bit
                flag = false;
            }
            if (sum >= 10) {  // If it is greater than 10, it is necessary to round off the tens, keep the ones, and mark the carry
                sum -= 10;
                flag = true;
            }
            ListNode *next_temp = new ListNode(sum);  // Create a new node
            temp->next = next_temp;  // Insert the node at the end of the original chain
            temp = next_temp;
        }
        if (l1->next != NULL) {  // If l1 is not finished and l2 is finished, add l1 to the answer next
            while (l1->next != NULL) {
                l1 = l1->next;
                int val = l1->val;
                if (flag) {
                    val++;
                    flag = false;
                }
                if (val >= 10) {
                    val -= 10;
                    flag = true;
                }
                ListNode *next_temp = new ListNode(val);
                temp->next = next_temp;
                temp = next_temp;
            }
        } else if (l2->next != NULL) {  // If l2 is not added and l1 is added, add l2 to the answer
            while (l2->next != NULL) {
                l2 = l2->next;
                int val = l2->val;
                if (flag) {
                    val++;
                    flag = false;
                }
                if (val >= 10) {
                    val -= 10;
                    flag = true;
                }
                ListNode *next_temp = new ListNode(val);
                temp->next = next_temp;
                temp = next_temp;
            }
        }
        if (flag) {  // If l1 and l2 are added, check whether there is carry. If so, add another node with a value of 1.
            ListNode *next_temp = new ListNode(1);
            temp->next = next_temp;
            temp = next_temp;
        }
        return ans;
    }
};

Posted by franzy_pan on Sun, 05 Apr 2020 05:00:10 -0700