Skip to content

Instantly share code, notes, and snippets.

@vikene
Created June 20, 2018 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikene/28b137533f60f84260a9c6f7d42e1731 to your computer and use it in GitHub Desktop.
Save vikene/28b137533f60f84260a9c6f7d42e1731 to your computer and use it in GitHub Desktop.
/* -----------------------------------
* WARNING:
* -----------------------------------
* Your code may fail to compile
* because it contains public class
* declarations.
* To fix this, please remove the
* "public" keyword from your class
* declarations.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int numericArray1[] = new int[2000];
int numericArray2[] = new int[20000];
int count1 = 0, count2 = 0;
for(int i=0;i<2000;i++){
numericArray1[i] =0;
numericArray2[i] = 0;
}
while(l1 != null){
numericArray1[count1] = l1.val;
count1++;
l1 = l1.next;
}
while(l2 != null){
numericArray2[count2] = l2.val;
count2++;
l2 = l2.next;
}
int value1 = 0, value2 = 0;
for(int i = count1-1; i >=0; i-- ){
value1 = value1 * 10 + numericArray1[i];
}
for(int i = count2 -1; i>=0; i--){
value2 = value2 * 10 + numericArray2[i];
}
System.out.println(value1+" "+value2);
int sumOfTwoNumbers = value1 + value2;
int lengthOffinal = String.valueOf(sumOfTwoNumbers).length();
ListNode head = new ListNode(0);
ListNode temp = head;
for(int i=lengthOffinal; i >0;i--){
temp.next = new ListNode(sumOfTwoNumbers%10);
sumOfTwoNumbers /= 10;
temp = temp.next;
}
head = head.next;
return head;
}
}
public class MainClass {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input);
// Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
}
public static String listNodeToString(ListNode node) {
if (node == null) {
return "[]";
}
String result = "";
while (node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
ListNode l1 = stringToListNode(line);
line = in.readLine();
ListNode l2 = stringToListNode(line);
ListNode ret = new Solution().addTwoNumbers(l1, l2);
String out = listNodeToString(ret);
System.out.print(out);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment