leecode两数相加

在这里插入图片描述

import java.util.*;
public class Main{

    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        long a=0;
        int ac=0;
        long b=0;
        int bc=0;
        long c=0;
        while(l1!=null){
            a+=l1.val*Math.pow(10,ac);
            ac++;
            l1=l1.next;
        }
        while(l2!=null){
            b+=l2.val*Math.pow(10,bc);
            bc++;
            l2=l2.next;            
        }
        c=a+b;

        //System.out.println(c);
        ListNode l[]=new ListNode[100];
        for(int i=0;i<l.length;i++) {
        	l[i]=new ListNode();
        }
        int i=0;
        while(c!=0){
        	l[i].val=(int)(c%10);
        	if(i>=1)
        		l[i-1].next=l[i];
            c=c/10;
        	i++;
        }
        
        return l[0];
    
    }
    public static void main(String[] args) {
        //Scanner in = new Scanner(System.in);
        //int a = in.nextInt();
        //System.out.println(a);
    	ListNode firstNode1=new ListNode(9);
    	ListNode secondNode1=new ListNode(9);
    	ListNode firstNode2= new ListNode(9);
    	ListNode secondNode2=new ListNode(9);
    	ListNode firstNode3=new ListNode(9);
    	ListNode secondNode3=new ListNode(9);
    	ListNode firstNode4= new ListNode(9);
    	ListNode secondNode4=new ListNode(9);
    	ListNode secondNode5=new ListNode(9);
        ListNode l1=new ListNode(9);
        ListNode l2=new ListNode(1);
        ListNode l3=new ListNode();
        l1.next=firstNode1;
        firstNode1.next=secondNode1;
        secondNode1.next=firstNode2;
        firstNode2.next=secondNode2;
        secondNode2.next=secondNode3;
        secondNode3.next=firstNode3;
        firstNode3.next=secondNode4;
        secondNode4.next=firstNode4;
        firstNode4.next=secondNode5;
        l3=addTwoNumbers(l1,l2);
        while(l3!=null){
            System.out.println(l3.val);
            l3=l3.next;
        }
        //System.out.println("Hello World!");
        
    }
}
class ListNode{
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val){this.val=val;}
    ListNode(int val,ListNode next){this.val=val;this.next=next;}
}

a hahahhaha,不行哦,,太浪费空间了,而且太大了,,还是得用进位来,,呵呵呵呵

class Solution {
    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        int carry=0;
        ListNode l[]=new ListNode[100];
        for(int i=0;i<l.length;i++) {
        	l[i]=new ListNode(0);
        }
        int i=0;
        while(l1!=null||l2!=null){
            int n1= l1!=null?l1.val:0;
            int n2= l2!=null?l2.val:0;
            int sum=n1+n2+carry;
            l[i].val=sum%10;
            if(i>=1)
                l[i-1].next=l[i];
            carry=sum/10;
            if(l1!=null)
                l1=l1.next;
            if(l2!=null)
                l2=l2.next;
            i++;

        }
        if(carry>0){
            l[i].val=carry;
            if(i>=1)
                l[i-1].next=l[i];            
        }        
        return l[0];
    
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值