[LeetCode #2 ] Add Two Numbers

Problem Description : Add Two Numbers

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

package adongre.solutions.leetcode;

public class ProblemOneTwoSum {
	public class ListNode {
		int val;
		ListNode next;

		ListNode(int x) {
			val = x;
			next = null;
		}
	}

	public class Solution {
		public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

			java.util.ArrayList<Integer> l1Array = new java.util.ArrayList<Integer>();
			java.util.ArrayList<Integer> l2Array = new java.util.ArrayList<Integer>();

			while (l1 != null) {
				l1Array.add(l1.val);
				l1 = l1.next;
			}

			while (l2 != null) {
				l2Array.add(l2.val);
				l2 = l2.next;
			}
			java.util.Collections.reverse(l1Array);
			java.util.Collections.reverse(l2Array);

			int pow = l1Array.size() - 1;
			long number1 = 0;
			long number2 = 0;
			for (Integer num : l1Array) {
				number1 += num.intValue() * Math.pow(10, pow--);
			}
			pow = l2Array.size() - 1;
			for (Integer num : l2Array) {
				number2 += num.intValue() * Math.pow(10, pow--);
			}
			long result = (number1 + number2);
			if (result == 0) {
				ListNode result1 = new ListNode(0);
				return result1;
			}
			java.util.ArrayList<Integer> r = new java.util.ArrayList<Integer>();
			while (result > 0) {
				r.add((int)(result % 10));
				result /= 10;
			}

			ListNode result1 = new ListNode(r.get(0));
			ListNode tempHead = result1;
			for (int i = 1; i < r.size(); i++) {
				ListNode tempNode = new ListNode(r.get(i));
				tempHead.next = tempNode;
				tempHead = tempNode;
			}

			return result1;
		}
	}

	public static void main(String[] args) {
		ProblemOneTwoSum p = new ProblemOneTwoSum();
		ProblemOneTwoSum.Solution s = p.new Solution();
		{
			ListNode A = p.new ListNode(0);
			ListNode B = p.new ListNode(1);
			ListNode r = s.addTwoNumbers(A, B);
		}

		{
			// {9}, {1,9,9,9,9,9,9,9,9,9}
			ListNode A = p.new ListNode(9);

			ListNode B = p.new ListNode(1);

			ListNode B91 = p.new ListNode(9);
			ListNode B92 = p.new ListNode(9);
			ListNode B93 = p.new ListNode(9);
			ListNode B94 = p.new ListNode(9);
			ListNode B95 = p.new ListNode(9);
			ListNode B96 = p.new ListNode(9);
			ListNode B97 = p.new ListNode(9);
			ListNode B98 = p.new ListNode(9);
			ListNode B99 = p.new ListNode(9);

			B.next = B91;
			B91.next = B92;
			B92.next = B93;
			B93.next = B94;
			B94.next = B95;
			B95.next = B96;
			B96.next = B97;
			B97.next = B98;
			B98.next = B99;
			ListNode r = s.addTwoNumbers(A, B);

		}

		{
			ListNode A = p.new ListNode(2);
			ListNode A4 = p.new ListNode(4);
			ListNode A3 = p.new ListNode(3);

			A.next = A4;
			A4.next = A3;

			ListNode B = p.new ListNode(5);
			ListNode B6 = p.new ListNode(6);
			ListNode B4 = p.new ListNode(4);

			B.next = B6;
			B6.next = B4;

			ListNode r = s.addTwoNumbers(A, B);

		}
		{
			ListNode A = p.new ListNode(0);
			ListNode B = p.new ListNode(0);

			ListNode r = s.addTwoNumbers(A, B);
		}
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s