Algorithm deliberate practice -LeetCode combat 10- two numbers together

Title: two numbers together

Link to the original question: two numbers together

The question is not complicated, but I think the same problem - the amount of code to reflect a very human level of programming:

His first to do the amount of code is like this (could not bear to look ~ ~):

/**
 * 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 *head = new ListNode();
        ListNode *p = head;
        int temp = 0;
        while(l1 != NULL && l2 != NULL)
        {
            ListNode *q = new ListNode();
            p->next = q;
            p = p->next;
            temp = l1->val + l2->val + temp;
            if(temp < 10)
            {
                p->val = temp; 
                temp = 0;
            }
            else
            {
                p->val = temp - 10;
                temp = 1;
            }  
            l1 = l1->next;
            l2 = l2->next;
        }
        while(l1 != NULL)
        {
            ListNode *q = new ListNode();
            p->next = q;
            p = p->next;
            temp = l1->val + temp;
            if(temp < 10)
            {
                p->val = temp; 
                temp = 0;
            }
            else
            {
                p->val = temp - 10;
                temp = 1;
            }
            l1 = l1->next;           
        }
        while(l2 != NULL)
        {
            ListNode *q = new ListNode();
            p->next = q;
            p = p->next;
            temp = l2->val + temp;
            if(temp < 10)
            {
                p->val = temp; 
                temp = 0;
            }
            else
            {
                p->val = temp - 10;
                temp = 1;
            }
            l2 = l2->next; 
        }
        if(temp == 1)
        {
            ListNode *q = new ListNode();
            p->next = q;
            p = p->next;
            p->val = 1;

        }
        return head->next;
    }
};

This is clearly not a time elegant code, then looked to find the official answer, and she looks refreshed. as follows:

/**
 * 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 *head = new ListNode();
        ListNode *p = head;
        int carry = 0;
        while(l1 != NULL || l2 != NULL)
        {
            int x = (l1 != NULL)?l1->val:0;
            int y = (l2 != NULL)?l2->val:0;
            int sum = carry + x + y;
            p->next = new ListNode(sum % 10);
            p = p->next;
            carry = sum / 10;
            if(l1 != NULL) l1 = l1->next;
            if(l2 != NULL) l2 = l2->next;
        }
        if(carry != 0)
            p->next =new ListNode(1);
        return head->next;
    }
};

Given that official java, I put him converted to C ++. Looks really quite beautiful ~

Digression:

He said the United States will spend some say "there are not beautiful flower," is expected to have such complaints, then wrote: "there are both beautiful flower is not beautiful flowers." This is basically nonsense, so that everyone agrees text can not be called expression.
- Koji Suzuki

Published 16 original articles · won praise 0 · Views 277

Guess you like

Origin blog.csdn.net/DZZ18803835618/article/details/104766656