2. leetcode algorithm title two numbers together

2. adding two numbers (Average)

We are given two non-empty list is used to represent two non-negative integer . Among them, their respective median is the reverse order of the way storage , and they each node can store only one digit .
If we these two numbers add up, 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.

示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

Method One: General approach

/**
 * 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) {
        int len1=1;//记录l1的长度
        int len2=1;//记录l2的长度
        ListNode* p=l1;
        ListNode* q=l2;
        while(p->next!=NULL)//获取l1的长度
        {
            len1++;
            p=p->next;
        }
        while(q->next!=NULL)//获取l2的长度
        {
            len2++;
            q=q->next;
        }
        if(len1>len2)//l1较长,在l2末尾补零
        {
            for(int i=1;i<=len1-len2;i++)
            {
                q->next=new ListNode(0);
                q=q->next;
            }
        }
        else//l2较长,在l1末尾补零
        {
            for(int i=1;i<=len2-len1;i++)
            {
                p->next=new ListNode(0);
                p=p->next;
            }
        }
        p=l1;
        q=l2;
        bool count=false;//记录进位
        ListNode* l3=new ListNode(-1);//存放结果的链表
        ListNode* w=l3;//l3的移动指针
        int i=0;//记录相加结果
        while(p!=NULL&&q!=NULL)
        {
            i=count+p->val+q->val;
            w->next=new ListNode(i%10);
            count=i>=10?true:false;
            w=w->next;
            p=p->next;
            q=q->next;
        }
        if(count)//若最后还有进位
        {
            w->next=new ListNode(1);
            w=w->next;
        }
        return l3->next; 
    }
};

L1 and 12 first calculate the length of the list, then allowed same length, the carry is stored in addition to l3
1. Get list corresponding to the length of the two; 2 end in a short zero padding list;.. 3 aligned addition considering the carry bit
method two: not aligned with zero padding, if the list is not empty then a sum (the result of representatives of each bit) plus, considering the carry bit

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(-1);//存放结果的链表
        ListNode* h=head;//移动指针
        int sum=0;//每个位的加和结果
        bool carry=false;//进位标志
        while(l1!=NULL||l2!=NULL)
        {
            sum=0;
            if(l1!=NULL)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2!=NULL)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            if(carry)
                sum++;
            h->next=new ListNode(sum%10);
            h=h->next;
            carry=sum>=10?true:false;
        }
        if(carry)
        {
            h->next=new ListNode(1);
        }
        return head->next;
    }
};

Optimization about Method Two:

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *dummy = new ListNode(-1), *cur = dummy;
        int carry = 0;
        while (l1 || l2) {
            int val1 = l1 ? l1->val : 0;
            int val2 = l2 ? l2->val : 0;
            int sum = val1 + val2 + carry;
            carry = sum / 10;
            cur->next = new ListNode(sum % 10);
            cur = cur->next;
            if (l1) l1 = l1->next;
            if (l2) l2 = l2->next;
        }
        if (carry) cur->next = new ListNode(1);
        return dummy->next;
    }
};

We create a new list, then enter the two lists from scratch back line and, every two addition, a new node is added to the back of the new list. After two inputs in order to avoid the list is empty at the same time, we construct a dummy node, the two nodes are added to generate a new node is added to dummy node order, since the dummy node itself can not be changed, so use a pointer cur list to point to the new last node. Well, you can start to make two lists are added, and this question is nice at the beginning of the lowest position in the list, it is possible to additionally direct order from low to high while traversing the list. Two lists conditions while loop as long as there is not a blank line, since the list may be empty, so when taking the current node value, to determine what, if set to 0 is empty, otherwise, it is the node value. Then the two values ​​are added nodes, while the carry plus carry. Then update carry, direct sum / 10 can be, and then sum% 10 establish a value of a new node, connected to the rear cur, cur and then moves to the next node. After two nodes then update, if present, points to the next location. After the while loop exits, the highest bit of the last special issues to deal with it, if the carry is 1, is to build a node 1 returns the new list to the next.

Published 40 original articles · won praise 10 · views 2271

Guess you like

Origin blog.csdn.net/weixin_44244154/article/details/104781588