Leetcode 002 Add-Numbers

Source: Internet
Author: User

ADD Numbersmy SubmissionsQuestionTotal accepted:105768 Total submissions:494750 difficulty:medium

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

Test instructions: Two numbers are added with a linked list. Java is not familiar. I'm going to refuel, cxq!.
 Public classSolution { Public StaticListNode addtwonumbers (listnode L1, ListNode L2) {ListNode L3=NewListNode (-1); ListNode L3head=L3; if(l1==NULL)            returnL2; if(l2==NULL)            returnL1; intCarry=0;  while(l1!=NULL|| l2!=NULL){            if(l1!=NULL) {Carry+=L1.val; L1=L1.next; }            if(l2!=NULL) {Carry+=L2.val; L2=L2.next; } L3.next=NewListNode (carry%10); Carry=carry/10; L3=L3.next; }                if(carry==1) {L3.next=NewListNode (1); }        returnL3head.next; }         Public Static voidMain (string[] args) {listnode L1=NewListNode (2); L1.next=NewListNode (4); L1.next.next=NewListNode (3); ListNode L2=NewListNode (5); L2.next=NewListNode (6); L2.next.next=NewListNode (4); ListNode Anwer=addtwonumbers (L1,L2); System.out.println (Anwer.val+ "" +anwer.next.val+ "" +anwer.next.next.val); }}

Leetcode 002 Add-Numbers

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.