Add Numbers (last pitch)

Source: Internet
Author: User
Tags add numbers

Last pitch!

There should be problems with the algorithm, can not use addition to operate, this will lead to overflow, but changed a bit, found that there is no need to use the queue, everything is very orderly and natural, the modified code:

Class Solution
{
Public
ListNode *addtwonumbers (ListNode *l1, ListNode *l2)
{
Long A=listtolong (L1);
Long B=listtolong (L2);
Long sum=a+b;
return longtolist (sum);
}

Long Listtolong (listnode* t1)
{
unsigned long sum = 0;
unsigned long temp = 1;
while (T1!=null)
{
sum = sum + t1->val*temp;
temp = temp * 10;
T1 = t1->next;
}
return sum;
}


listnode* longtolist (unsigned long T)
{
listnode* t2 = new ListNode (0);
listnode* T1 = T2;
while (t! = 0)
{
T2->val = t% 10;
if (t/10!=0)
{
listnode* t3 = new ListNode (0);
T2->next = t3;
t2 = t3;
}
t = T/10;
}
return t1;
}
};

Add Numbers (last pitch)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.