前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Add Two Numbers

Add Two Numbers

作者头像
全栈程序员站长
发布2022-07-05 10:38:08
2820
发布2022-07-05 10:38:08
举报

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

错误解法:由于数太长。用int,long存不下,导致结果错误。

代码语言:javascript
复制
/**
 * 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;
    }
}

正确解法。空间效率极差。肯定还有更好的解法,没工夫研究,待续……

代码语言:javascript
复制
/**
 * 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;
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109425.html原文链接:https://javaforall.cn

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021年9月7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com