Leetcode02-add the Beats98.68%java version of Numbers

Source: Internet
Author: User

My Leetcode defeated the 90% trip, which was mainly done using the Java implementation algorithm to reach the 10% camp. This is the second add two Numbers


All code Download:
GitHub Link: GitHub link, click Surprise;
Writing articles is not easy, welcome everyone to take my article, as well as give useful comments, of course, you can also pay attention to my GitHub;

1. Topic Description: English, translation find Youdao

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

2. My train of thought:

1. The topic needs to be done is to put two linked lists to sum up the position.
2. My solution is to store the sum value in the L2 linked list.
3. When L2 is null, you need to point L2 's next to L1 (L1 is not empty).
4. It is important to note that when L1 and L2 are empty, a new node is required if the carry is 1.

3. My Code
/** * * @author Peace idea: The topic needs to do is to put two linked list of the sum, the position. My solution is to store the sum of values in the L2 linked list. When L2 to null, you need to point L2 's next to L1. It is important to note that when both L1 and L2 are empty, if the carry is 1, a new node is required. */ Public  class addtwonum {} class listnode {      intVal      ListNode Next; ListNode (intX) {val = x;} } class solution {     PublicListNode addtwonumbers (listnode L1, ListNode L2) {//Boundary Determination        if(l1==NULL)returnL1;if(l2==NULL)returnL2;intk=0;//Rounding        int sum=0;//AndListNode Temp=l2; ListNode Head=l2;//Use L2 to store summed values         while(l2!=NULL) {temp=l2;if(l1!=NULL)//l1 is not empty when the two add{sum=l1.val+l2.val+k;           L1=l1.next; }Else{when//l1 is empty, only the L2 is summed              if(k==0) Break;Else{sum=l2.val+k; }           }//On Carry judgment            if(sum>=Ten) {k=1;sum=sum-Ten; }Else{k=0; }//Sum value saved to linked listTemp.val=sum;        L2=l2.next; }if(k!=0)//Jump out of L2 loop to determine if K is 0{temp.next=l1; while(l2==NULL&&l1!=NULL){//When the L1 is not empty, the L1 is solvedTEMP=L1;sum=l1.val+k;if(sum>=Ten) {k=1;sum=sum-Ten; }Else{k=0; } temp.val=sum;         L1=l1.next; }if(k!=0)//l1 and L2 are empty, new nodes{ListNode node=NewListNode (k);        Temp.next=node; }       }Else if(l1!=NULL){//Carry is empty and L1 is not empty when connected directly to L1TEMP.NEXT=L1; }returnHead }}

The results of my accept:

4. Used Inefficient code:
 Public classSolution { PublicListNodeaddtwonumbers(ListNode L1, ListNode L2) {ListNode Dummyhead =NewListNode (0); ListNode p = L1, q = L2, Curr = Dummyhead;intcarry =0; while(P! =NULL|| Q! =NULL) {intx = (P! =NULL) ? P.val:0;inty = (Q! =NULL) ? Q.val:0;intsum = carry + x + y; carry = SUM/Ten; Curr.next =NewListNode (sum%Ten); Curr = Curr.next;if(P! =NULL) p = p.next;if(Q! =NULL) q = q.next; }if(Carry >0) {Curr.next =NewListNode (carry); }returnDummyhead.next;}}

Okay, here's what this chapter is about.
From Wpeace (rlovep.com)

Leetcode02-add the Beats98.68%java version of 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.