|
| 1 | +package revision; |
| 2 | + |
| 3 | +public class matchNumberLL { |
| 4 | + |
| 5 | + |
| 6 | + public static boolean CheckRepresentaion(ListNode<Integer> head1, ListNode<Integer> head2) { |
| 7 | + |
| 8 | + if (head1 == null && head2 == null) { |
| 9 | + return true; |
| 10 | + } |
| 11 | + |
| 12 | + if (head1 == null || head2 == null) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + |
| 16 | + StringBuilder num1 = new StringBuilder(); |
| 17 | + StringBuilder num2 = new StringBuilder(); |
| 18 | + |
| 19 | + |
| 20 | + while (head1 != null && head2 != null) { |
| 21 | + |
| 22 | + num1.append(head1.data); |
| 23 | + num2.append(head2.data); |
| 24 | + head1 = head1.next; |
| 25 | + head2 = head2.next; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + while (head1 != null) { |
| 32 | + num1.append(head1.data); |
| 33 | + head1 = head1.next; |
| 34 | + } |
| 35 | + while (head2 != null) { |
| 36 | + num2.append(head2.data); |
| 37 | + head2 = head2.next; |
| 38 | + } |
| 39 | + while (num1.charAt(0) == '0') { |
| 40 | + num1.deleteCharAt(0); |
| 41 | + } |
| 42 | + while (num2.charAt(0) == '0') { |
| 43 | + num2.deleteCharAt(0); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + return (num1.toString().compareTo(num2.toString()) != 0) ? false : true; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public static String convert(int n) { |
| 53 | + |
| 54 | + if (n == 0) { |
| 55 | + return "0"; |
| 56 | + } |
| 57 | + String s = ""; |
| 58 | + char t; |
| 59 | + while (n > 0) { |
| 60 | + t = (char) (n % 10); |
| 61 | + s = t + s; |
| 62 | + n /= 10; |
| 63 | + } |
| 64 | + |
| 65 | + return s; |
| 66 | + } |
| 67 | + |
| 68 | + // problem with this method...c++ |
| 69 | + public static boolean method(ListNode<Integer> a, ListNode<Integer> b) { |
| 70 | + |
| 71 | + String one = "", two = ""; |
| 72 | + |
| 73 | + boolean zero = false; |
| 74 | + |
| 75 | + while (a.next != null) { |
| 76 | + |
| 77 | + zero = true; |
| 78 | + if (a != null || one.length() != 0) { |
| 79 | + one += convert(a.data); |
| 80 | + } |
| 81 | + a = a.next; |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + if (zero == true && one.length() != 0) { |
| 86 | + one = "0"; |
| 87 | + } |
| 88 | + zero = false; |
| 89 | + |
| 90 | + while (b.next != null) { |
| 91 | + zero = true; |
| 92 | + if (b != null || two.length() != 0) { |
| 93 | + two += convert(b.data); |
| 94 | + } |
| 95 | + |
| 96 | + b = b.next; |
| 97 | + } |
| 98 | + |
| 99 | + if (zero == true && two.length() != 0) { |
| 100 | + two = "0"; |
| 101 | + } |
| 102 | + |
| 103 | + return one.equals(two); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + public static void main(String[] args) { |
| 108 | + |
| 109 | + ListNode<Integer> head = new ListNode<>(1); |
| 110 | + ListNode<Integer> head2 = new ListNode<Integer>(14); |
| 111 | + |
| 112 | + System.out.println(CheckRepresentaion(head, head2)); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments