Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /* -----------------------------------
  2. * WARNING:
  3. * -----------------------------------
  4. * Your code may fail to compile
  5. * because it contains public class
  6. * declarations.
  7. * To fix this, please remove the
  8. * "public" keyword from your class
  9. * declarations.
  10. */
  11.  
  12. /**
  13. * Definition for singly-linked list.
  14. * public class ListNode {
  15. * int val;
  16. * ListNode next;
  17. * ListNode(int x) { val = x; }
  18. * }
  19. */
  20. class Solution {
  21. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  22. int numericArray1[] = new int[2000];
  23. int numericArray2[] = new int[20000];
  24. int count1 = 0, count2 = 0;
  25. for(int i=0;i<2000;i++){
  26. numericArray1[i] =0;
  27. numericArray2[i] = 0;
  28. }
  29. while(l1 != null){
  30. numericArray1[count1] = l1.val;
  31. count1++;
  32. l1 = l1.next;
  33. }
  34. while(l2 != null){
  35. numericArray2[count2] = l2.val;
  36. count2++;
  37. l2 = l2.next;
  38. }
  39. int value1 = 0, value2 = 0;
  40. for(int i = count1-1; i >=0; i-- ){
  41. value1 = value1 * 10 + numericArray1[i];
  42. }
  43. for(int i = count2 -1; i>=0; i--){
  44. value2 = value2 * 10 + numericArray2[i];
  45. }
  46. System.out.println(value1+" "+value2);
  47. int sumOfTwoNumbers = value1 + value2;
  48. int lengthOffinal = String.valueOf(sumOfTwoNumbers).length();
  49. ListNode head = new ListNode(0);
  50. ListNode temp = head;
  51. for(int i=lengthOffinal; i >0;i--){
  52. temp.next = new ListNode(sumOfTwoNumbers%10);
  53. sumOfTwoNumbers /= 10;
  54. temp = temp.next;
  55. }
  56. head = head.next;
  57. return head;
  58. }
  59. }
  60.  
  61. public class MainClass {
  62. public static int[] stringToIntegerArray(String input) {
  63. input = input.trim();
  64. input = input.substring(1, input.length() - 1);
  65. if (input.length() == 0) {
  66. return new int[0];
  67. }
  68.  
  69. String[] parts = input.split(",");
  70. int[] output = new int[parts.length];
  71. for(int index = 0; index < parts.length; index++) {
  72. String part = parts[index].trim();
  73. output[index] = Integer.parseInt(part);
  74. }
  75. return output;
  76. }
  77.  
  78. public static ListNode stringToListNode(String input) {
  79. // Generate array from the input
  80. int[] nodeValues = stringToIntegerArray(input);
  81.  
  82. // Now convert that list into linked list
  83. ListNode dummyRoot = new ListNode(0);
  84. ListNode ptr = dummyRoot;
  85. for(int item : nodeValues) {
  86. ptr.next = new ListNode(item);
  87. ptr = ptr.next;
  88. }
  89. return dummyRoot.next;
  90. }
  91.  
  92. public static String listNodeToString(ListNode node) {
  93. if (node == null) {
  94. return "[]";
  95. }
  96.  
  97. String result = "";
  98. while (node != null) {
  99. result += Integer.toString(node.val) + ", ";
  100. node = node.next;
  101. }
  102. return "[" + result.substring(0, result.length() - 2) + "]";
  103. }
  104.  
  105. public static void main(String[] args) throws IOException {
  106. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  107. String line;
  108. while ((line = in.readLine()) != null) {
  109. ListNode l1 = stringToListNode(line);
  110. line = in.readLine();
  111. ListNode l2 = stringToListNode(line);
  112.  
  113. ListNode ret = new Solution().addTwoNumbers(l1, l2);
  114.  
  115. String out = listNodeToString(ret);
  116.  
  117. System.out.print(out);
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment