MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / addTwoNumbers

Method addTwoNumbers

Java/Add-Two-Numbers.java:16–59  ·  view source on GitHub ↗
(ListNode l1, ListNode l2)

Source from the content-addressed store, hash-verified

14
15class Solution {
16 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
17 ListNode currHead1 = l1;
18 BigInteger list1 = new BigInteger("0");
19 BigInteger x = new BigInteger("1");
20 String s;
21
22 while(currHead1 != null){
23 s = "" + (x.multiply(BigInteger.valueOf(currHead1.val)));
24 list1 = list1.add(new BigInteger(s));
25 currHead1 = currHead1.next;
26 x=x.multiply(BigInteger.valueOf(10));
27 }
28 //System.out.println(list1);
29
30 x = new BigInteger("1");
31 ListNode currHead2 = l2;
32 BigInteger list2 = new BigInteger("0");
33
34 while(currHead2 != null){
35 s = "" + (x.multiply(BigInteger.valueOf(currHead2.val)));
36 list2 = list2.add(new BigInteger(s));
37 currHead2 = currHead2.next;
38 x=x.multiply(BigInteger.valueOf(10));
39 }
40 //System.out.println(list2);
41 list2 = list2.add(list1);
42
43 //System.out.println(list2);
44 StringBuffer sb = new StringBuffer(""+list2+"");
45 sb.reverse();
46 //System.out.println(sb.toString());
47 String str = sb.toString();
48
49 ListNode result = new ListNode(Integer.parseInt(str.charAt(0)+""));
50 ListNode head = result;
51
52 for(int i=1;i<str.length();i++){
53 head.next = new ListNode(Integer.parseInt(str.charAt(i)+""));
54 //System.out.println(head.next.val);
55 head = head.next;
56 }
57
58 return result;
59 }
60}

Callers

nothing calls this directly

Calls 3

lengthMethod · 0.80
addMethod · 0.45
reverseMethod · 0.45

Tested by

no test coverage detected