Brush Leetcode2-add and Numbers together

Source: Internet
Author: User
Tags add numbers

Today do not go into the paper, also learn not to go into the new technology, so first brush the title, a will fill other.

-----------------------------------------------------I'm not a split line-------------------------------------------------

ADD Numbers

You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order

And each of the their nodes contain a single digit. ADD the numbers and return it as a linked list.

Input: (2, 4, 3) + (5, 6, 4)
Output:7, 0, 8

"Test Instructions": is to give you two lists, the contents of the list is a multi-digit reverse, such as 342 is represented as (2, 4, 3), now let you calculate the number of two multi-digit and,

At the same time, the same method is expressed, that is, the reverse order with the linked list.

"Heart Course" to see the topic at the beginning of the idea is to examine the simulation of addition + linked list operation of the problem, but also simple, linked list operation is a tail-linked list method.

So the code is started, and after the completion of a settlement found to appear re (TAT). The error example is [0],[0]. Think for a long while don't know where wrong ...

I found my head pointer was not allocated space, the amount, changed the turn, AC (^ ^)

--------------------------------------------------------------------------------------------------------------- ---------

The code is as follows:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * struct ListNode *next;6  * };7  */8 structlistnode* AddTwoNumbers (structlistnode* L1,structlistnode*L2) {9     structListNode * res = (structListNode *)malloc(sizeof(structlistnode));Ten     structListNode * ans =NULL; One     intJudge =0; A     intSave =0; -     intadd1,add2,sum; -     if(L1 = = NULL && L2 = = null)returnNULL; the      while(L1! = NULL | | L2! =NULL) { -         if(L1 = =NULL) { -ADD1 =0; -ADD2 = l2->Val; +L2 = l2->Next; -}Else if(L2 = =NULL) { +ADD1 = L1Val; AADD2 =0; atL1 = l1->Next; -}Else { -ADD1 = l1->Val; -ADD2 = l2->Val; -L1 = l1->Next; -L2 = l2->Next; in         } -sum = add1 + ADD2 +Save; toSave = SUM/Ten; +         structListNode * temp = (structListNode *)malloc(sizeof(structlistnode)); -Temp->val = sum%Ten; theTemp->next =NULL; *Res->next =temp; $         if(Judge = =0) {Panax NotoginsengAns =temp; -Judge =1; the         } +res = res->Next; A     } the     if(save) { +         structListNode * temp = (structListNode *)malloc(sizeof(structlistnode)); -Temp->val =Save; $Temp->next =NULL; $Res->next =temp; -     } -     returnans; the}

Brush Leetcode2-add and Numbers together

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.