两个数相加

 

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TowNumAcount {
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Integer sum = listNodeToInt(l1)+listNodeToInt(l2);
        int temp[] = this.dilicate(sum);
         ListNode result = new ListNode(temp[0]);
        for(int i=1 ;i < temp.length;i++){
            //System.out.println(temp[i]);
            result = this.addListNode(temp[i],result);
        }

         return result;
    }
    public ListNode addListNode(int val,ListNode ln){
         ListNode nextNode =new ListNode(val);
         ListNode tempNode = ln;
         while(tempNode.next != null){
            tempNode = tempNode.next;
         }
         tempNode.next = nextNode;

         return ln;
    }

    public int[] dilicate(int number){
        int temp;
        int index = (int) Math.log10(number)+1;
        int res[] = new int[index];
        int carry = this.count(index);
        for(int i=carry;i >= 1 ;i=i/10){
            temp = number / i;
            number = number % i;
            //System.out.println(temp);
            res[index-1] = temp;
            index--;
        }
    return res;
    }

   public int listNodeToInt(ListNode ln){
        HashMap<Integer,Integer> carryOver = new HashMap();
        List<Integer> integerList = new ArrayList<>();
        ListNode nextNode = ln;
       int temp = 1;
       int sum = 0;
       int length = 0;
       if(ln != null){
           length = 1;
           while( nextNode != null){
               length++;
               integerList.add(nextNode.val);
               nextNode = nextNode.next;
           }//end while
        }//end if
        for(int i=1 ; i <= length;i++){
           carryOver.put(i,this.count(i));
       }

       for(int i : integerList){
            sum=sum + i * carryOver.get(temp);
            temp++;
        }
       //System.out.println(sum);
       return sum;
   }

   public int count(int length){
       int temp = 10;
       int result = 1;
       for(int j = 1;j <= length;j++){
           result =result * temp;
       }
       return result/10;
   }



   public static void main(String args[]) {
       TowNumAcount tna = new TowNumAcount();
       ListNode ln11 = new ListNode(2);
       ListNode ln12 = new ListNode(4);
       ListNode ln13 = new ListNode(3);
       ln11.next = ln12;
       ln12.next = ln13;
       ln13.next = null;
       ListNode ln21 = new ListNode(5);
       ListNode ln22 = new ListNode(6);
       ListNode ln23 = new ListNode(4);
       ln21.next = ln22;
       ln22.next = ln23;
       ln23.next = null;
       ln11 = tna.addTwoNumbers(ln11,ln21);
       System.out.println(tna.listNodeToInt(ln11));
       //System.out.println(tna.count(8));
   }
}
public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值