LeetCode algorithm Question 2 record - two numbers together

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.
If we add up these two numbers, it will return a new list and to represent them.
You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Cause: 342 + 465 = 807

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/add-two-numbers
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

This problem no difficulty, but also exposed me to write code that is not trivial to write simple, there is greater room for optimization.
I am chicken dish is simple and not trivial algorithm :()

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* front;//front
        ListNode* rear;//rear
        int i;
        int c=0;//overflow
        i=l1->val+l2->val;
        if(i>9){
            c=1;
            i=i%10;
        }
        ListNode* l5=new ListNode(i);
        front=l5;
        rear=l5;
        l1=l1->next;
        l2=l2->next;

        
        while(l1!=NULL && l2!=NULL){
            i=l1->val+l2->val+c;
            c=0;
            if(i>9){
                c=1;
                i=i%10;
            }
            l5=new ListNode(i);
            rear->next=l5;
            
            rear=rear->next;
            l1=l1->next;
            l2=l2->next;
        }

        if(l1==NULL){
            while(l2!=NULL){
                i=l2->val+c;
                c=0;
                if(i>9){
                    c=1;
                    i=i%10;
                }
                rear->next=new ListNode(i);
                l2=l2->next;
                rear=rear->next;
            }
        }
        else if(l2==NULL){
            while(l1!=NULL){
                i=l1->val+c;
                c=0;
                if(i>9){
                    c=1;
                    i=i%10;
                }
                rear->next=new ListNode(i);
                l1=l1->next;
                rear=rear->next;
            }
            
        }
        if(c==1){
            rear->next=new ListNode(1);
            rear=rear->next;
        }
        return front;
    }
};

Currently users discover the best non-recursive version of the code (concise and takes up less memory):

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode vHead(0), *p = &vHead;
        int flag = 0;
        while (l1 || l2 || flag) {
            int tmp = 0;
            if (l1 != nullptr) tmp += l1->val;
            if (l2 != nullptr) tmp += l2->val;
            tmp += flag;
            
            flag = tmp / 10;
            tmp %= 10;
            
            ListNode *next = l1 ? l1 : l2;
            if (next == nullptr) next = new ListNode(tmp);
            next->val = tmp;
            
            p->next = next;
            p = p->next;
            l1 = l1 ? l1->next : nullptr;
            l2 = l2 ? l2->next : nullptr;
        }
        return vHead.next;
    }
};
Released two original articles · won praise 0 · Views 105

Guess you like

Origin blog.csdn.net/qq_44310853/article/details/103939958