Source: Internet
Author: User

You are given two linked lists representing two non-negative numbers. the digits are stored in reverse order and each of their nodes contain a single digit. add the two numbers and return it as a linked list.

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

Incorrect solution: the result is incorrect because the number is too long and INT and long cannot be stored.

`/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {        if(l1==null) return l2;        if(l2==null) return l1;        ListNode node1=l1;        ListNode node2=l2;        int a =0,b=0;        int c=1;        while(node1!=null) {        a+=(c*node1.val);        c=c*10;        node1=node1.next;        }        c=1;        while(node2!=null){        b+=(c*node2.val);        c=c*10;        node2=node2.next;        }        int d=a+b;                String res=Integer.toString(d);        ListNode l3=new ListNode(0);        ListNode node3=l3;        for(int i=res.length()-1;i>=0;i--){        node3.val=(int)(res.charAt(i))-(int)('0');        if(i>0){        node3.next=new ListNode(0);        node3=node3.next;        }        }        return l3;    }}`

Correct Solution, space efficiency is very poor, there must be a better solution, no time to study, to be continued ......

`/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {        if(l1==null) return l2;        if(l2==null) return l1;        ListNode node1=l1;        ListNode node2=l2;        List<Integer> list1=new ArrayList<Integer>();        List<Integer> list2=new ArrayList<Integer>();                while(node1!=null) {            list1.add(node1.val);            node1=node1.next;        }               while(node2!=null){            list2.add(node2.val);            node2=node2.next;        }                while(list1.size()<list2.size()) list1.add(0);        while(list2.size()<list1.size()) list2.add(0);                ListNode l3=new ListNode(0);        ListNode node3=l3;        int tmp=0;        for(int i=0;i<list1.size();i++){            int a=list1.get(i)+list2.get(i)+tmp;            if(a>=10){                node3.val=a-10;                tmp=1;            }            else{                node3.val=a;                tmp=0;            }            if(i<list1.size()-1){                node3.next=new ListNode(0);                node3=node3.next;            }        }                if(tmp==1){            node3.next=new ListNode(tmp);        }        return l3;    }}`

Related Keywords:

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.