Leetcode "2". ADD the Numbers--java implementation

Source: Internet
Author: User
Tags add numbers


second questionADD Numbers as follows:

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

simply put, the elements are added backwards and stored with the same rules for two unidirectional linked lists. Pay attention to rounding! Here is my Java program:One, the general practice: one-off calculation, and consider some of the situation to reach the end of the chain.
/** * Definition for singly-linked list. * public class ListNode {* int val, * ListNode next; * ListNode (in x) {val = x;}}} */public class Solutio        n {public ListNode addtwonumbers (listnode L1, ListNode L2) {ListNode slist = new ListNode (0);        ListNode clist = slist;        ListNode nlist = new ListNode (0);        int sval = 0; int flag = 0; Rounding//1. If first Node of L1 or L2 is null if (l1==null| | L2==null) {return (l1==null)? ( (l2==null)?        (slist):(L2)):(L1);            //2.1 when L1,L2 is not a chain tail, loop while (true) {Clist.val = (L1.val + l2.val + flag)% (10);            Flag = (L1.val + l2.val + flag)/10;            Next node L1 = L1.next;                        L2 = L2.next; 2.1.1 If any one is the end of the chain, jump out if (l1==null| |            L2==null) {break;            }else{clist.next= New ListNode (0);            CList =clist.next; }};//while               2.2 If two is at the same time as the end of the chain if (l1==null&&l2==null) {//2.2.1 If two is a chain tail and has carry, the result will be rounded               if (flag==1) {nlist = new ListNode (flag);            Clist.next = nlist;            }else{return slist;            }}else//2.2 one reaches the end of the chain, one not yet {ListNode onelist = new ListNode (0);            if (l1==null) {onelist = L2;            }else {onelist = L1;}              while (onelist!= null) {Clist.next = new ListNode (0);              CList = Clist.next;              Clist.val = (onelist.val + flag)%10;              Flag = (Onelist.val + flag)/10;            Onelist = Onelist.next; }//2.2.1 when the other one also reaches the end of the chain, judging if there is a carry if (flag==1) {clist.next = new ListNode (flag)            ;            }} return slist; }}

second, clear-minded approach: to read the linked list first as a numeric type, add the result to the specified list. The idea of this method is very clear and simple, but whether it will overflow, time and space complexity increase and so on. Third, there is a better and more concise solution for reference::leetcode–add-Numbers (Java). This method determines whether the two linked lists are to the end of the chain. There is no need to consider a variety of situations like one, and many problems seem to have a way of unifying situations. Think more about whether this is the way to go before you do it next time.


Leetcode "2". ADD the Numbers--java implementation

Related Article

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.