Leetcode_2_ problem--add The Numbers (linked list)

Source: Internet
Author: User
Tags add numbers

ADD NumbersTotal accepted:58510 Total submissions:268082my submissions QuestionSolution

You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order and all of 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

Hide TagsLinked List MathHas you met this question in a real interview? Yes No

Discuss

#include <iostream>using namespace std;struct listnode {int val; ListNode *next; ListNode (int x): Val (x), Next (NULL) {}}; listnode* addtwonumbers (listnode* L1, listnode* L2) {listnode* ptr1=l1; listnode* Ptr2=l2; listnode* Head=null; listnode* ptr;int jinwei=0;if (ptr1==null&&ptr2==null)//If all are empty then return directly to Head;head= (listnode*) malloc ( sizeof (ListNode)); if (ptr1!=null&&ptr2!=null)//two are not empty {head->val= (ptr1->val+ptr2->val)%10; Jinwei= (Ptr1->val+ptr2->val)/10;} else if (ptr1==null&&ptr2!=null)//either is empty, or you can return directly to the non-empty linked list to return Ptr2;elsereturn ptr1;head->next=null; ptr=head;//Below is the first of the two linked lists is not empty while (1) {//here if one is empty then no longer moves backwards, if (ptr1!=null) ptr1=ptr1->next;if (ptr2!=null) ptr2= Ptr2->next;if (ptr1==null&&ptr2==null)//If two lists have reached the last empty place, you can calculate the jump out loop {if (jinwei==1) {listnode* temp2= ( listnode*) malloc (sizeof (ListNode)); temp2->val=1;temp2->next=null;ptr->next=temp2;ptr=temp2;} break;} Here it is stated that there must be at least one not empty and that you need to continue the calculation to continue the loop int a=0,b=0;if (ptr1!=null) a=ptr1->val;if (PTR2!=null) b=ptr2->val; listnode* temp= (listnode*) malloc (sizeof (ListNode)), temp->val= (A+b+jinwei)%10;jinwei= (A+b+jinwei)/10;temp- >next=null;ptr->next=temp;ptr=temp;} return head;} int main () {listnode* l1= (listnode*) malloc (sizeof (ListNode));l1->next=null;l1->val=0; listnode* l2= (listnode*) malloc (sizeof (ListNode)), l2->val=7;l2->next= (listnode*) malloc (sizeof (ListNode)); l2->next->val=3;l2->next->next=null; listnode* l3=addtwonumbers (L1,L2);}

  

Leetcode_2_ problem--add The Numbers (linked list)

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.