Leetcode:add two Numbers-list of two linked lists with carry-on additions to create a new list

Source: Internet
Author: User

1. Title

Add two Numbers (two linked lists are made with carry-on addition to create a new linked list)

2. Address of the topic

https://leetcode.com/problems/add-two-numbers/

3. Topic content

English: 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.

Chinese: Given two linked lists, where the elements are non-negative integers. Add the corresponding positions of the two linked lists, and carry the rounding to the next set of additions if the two numbers are added with rounding. Returns the new linked list after the addition.

For example: The list given is (2, 4, 3) and (5, 6, 4), the result is: 7, 0, 8

4. Method of solving Problems 1

My first idea was to set up two linked lists for L1 and L2, and then to go backwards through the two linked lists. The following three points are to be considered for each traversal:

1, consider L1 and L2 have a link table corresponding position is empty situation

2, consider the new link table root node and non-root node situation

3, consider the situation of rounding

A section of Java code that implements this method is as follows:

/** *  function Description:leetcode 2 - add two numbers *  Developer: Tsybius *   Development time: August 27, 2015  */public class Solution {         /**     *  add two linked lists together, generate a new list, add each and more than 10 to the next       *  @param  l1  linked list 1     *  @param  l2  linked list 2      *  @return   New link list      */    public  Listnode addtwonumbers (LISTNODE L1, LISTNODE L2)  {                 ListNode listNodeRoot = null;         ListNode listNodeCurr = listNodeRoot;         int carry = 0;                 while  (l1 != null | |  l2 != null)         {             //need to consider L1 and L2 have an empty case              int sum = 0;             if  (l1 != null)  {                 sum += l1.val;             }            if   (L2 != null)  {                 sum += l2.val;             }            sum += carry;                         //need to consider the case of root and non-root nodes              if (listnoderoot == null)              {                 listnodecurr = new listnode (sum);                 listNodeRoot = listNodeCurr;             }             else             {                 Listnodecurr.next = new&nBsp ListNode (sum);                 listnodecurr = listnodecurr.next;             }                         //need to consider the case of rounding              carry = 0;             while  (listnodecurr.val >= 10)  {                 listNodeCurr.val -= 10;                 carry += 1;             }                          //backwards Traversal              if (l1 != null)     {                 l1 = l1.next;             }             if (l2 != null)     {                 l2 = l2.next;             }        }                 //consider the last addition of a carry case          if  (carry > 0)  {         &nbsP;   listnodecurr.next = new listnode (Carry);         }                 return listnoderoot;    }}

But later wrote a more concise code, the following Java code of the general idea and the previous code has been, but reduced the judgment, improve efficiency.

/** *  function Description:leetcode 2 - add two numbers *  Developer: Tsybius *   Development time: August 27, 2015  */public class Solution {         /**     *  add two linked lists together, generate a new list, add each and more than 10 to the next       *  @param  l1  linked list 1     *  @param  l2  linked list 2      *  @return   New link list      */      public  listnode addtwonumbers (LISTNODE L1, LISTNODE L2)  {                 listnode listnoderoot =  new listnode (0);         listnode listnodecurr =  listNodeRoot;        int carry = 0;                 while  (l1 != null | |  l2 != null | |  carry != 0)         {             //need to consider L1 and L2 have an empty case              int sum =  (l1 != null ? l1.val : 0)   +  (l2 != null ? l2.val : 0)  + carry;             //calculate current bit and carry              listnodecurr.next = new listnode (sum % 10);             carry = sum / 10;                           //backwards Traversal             listNodeCurr =  listnodecurr.next;            l1 =  (L1  != null ? l1.next : null);             l2 =  (l2 != null ? l2.next : null);         }                 return listnoderoot.next;    }}

END

Leetcode:add two Numbers-list of two linked lists with carry-on additions to create a new 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.