Leetcode-2 ADD two Numbers calculate the list of two pairs of problems

Source: Internet
Author: User



1. Description of the problem:

You are given, linked, lists representing, and non-negativenumbers. The digits is stored in reverse order and each of the their nodes containa a single digit. ADD the numbers and return it as a linked list.

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

Definition of a linked list:

/** * Definition for singly-linked list. * public class ListNode {*     int val; *     ListNode Next; *     listnode (int x) {*         val = x; *         next = nul L *     } * } */


Here are a few things to consider: Considering that two linked lists are inconsistent in length, and that the list is still rounded up.


Solution One: The more tedious classification discussion method, avoids the use


public class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) {if ((l1==null) | |        (L2==null))        {return null;        } int temp_val = L1.val + l2.val;        int go = TEMP_VAL/10;         ListNode result = new ListNode (TEMP_VAL%10);        L1 = L1.next;        L2 = L2.next;        ListNode temp = result;            while ((L1!=null) && (l2!=null)) {temp_val = L1.val + L2.val + go;            ListNode temp2 = new ListNode (TEMP_VAL%10);             Temp.next = Temp2;            temp = TEMP2;            L1 = L1.next;            L2 = L2.next;        Go = TEMP_VAL/10;            } while (l1!=null) {temp_val = L1.val + go;            ListNode temp2 = new ListNode (TEMP_VAL%10);             Temp.next = Temp2;            temp = TEMP2;            L1 = L1.next;        Go = TEMP_VAL/10;            } while (l2!=null) {temp_val = L2.val + go; LiStnode temp2 = new ListNode (TEMP_VAL%10);             Temp.next = Temp2;            temp = TEMP2;            L2 = L2.next;        Go = TEMP_VAL/10;        } if (Go! = 0) {Temp.next = new ListNode (go);    } return result; }}

Solution Two: The better solution, but the running time on the Leetcode is inferior to the former

public class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) {    int carry = 0;//denotes carry    listnode hea D = new ListNode (0);//First node, refer to C + + post-tail node    listnode temp = head;    Loop until the two-linked list is traversed completely before exiting while    ((L1! = null) | | (L2! = null))    {    //Only a few factor sizes to be determined;    int first = (L1! = null)? l1.val:0;//use the IF statement also    int second = (L2! = null)? l2.val:0;
   int result = first + second + carry;//each cycle evaluates    carry = RESULT/10;    ListNode pnode = new ListNode (result%);    Temp.next = Pnode;    temp = pnode;//Prepares the next loop        if (L1! = null) {L1 = L1.next;}        if (L2! = null) {L2 = L2.next;}        }    Also consider the case where CArray is not equal to 0 and still has rounding    if (Carry > 0)    {    temp.next = new ListNode (carry);    }        Return head.next;//Note This return value    }}





Leetcode-2 ADD two Numbers calculate the list of two pairs of problems

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.