1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0);
ListNode t = dummy;
int carry = 0;
ListNode p = l1; ListNode q = l2; while (p != null || q != null) { int x = p == null ? 0 : p.val; int y = q == null ? 0 : q.val; int value = x + y + carry;
carry = value > 9 ? 1 : 0; value = value > 9 ? value - 10 : value;
t.next = new ListNode(value); t = t.next;
p = p == null ? p : p.next; q = q == null ? q : q.next; }
if (carry > 0) { int value = carry;
t.next = new ListNode(value); t = t.next; }
return dummy.next; }
static class ListNode { int val; ListNode next;
ListNode(int x) { val = x; next = null; }
public static ListNode genLinkedList(int[] nums, boolean withHead, int loopPos) { if (nums.length == 0) { return null; }
int val = withHead ? Integer.MIN_VALUE : nums[0]; ListNode head = new ListNode(val);
ListNode loop = withHead ? null : loopPos == 0 ? head : null;
int b = withHead ? 0 : 1; ListNode p = head; for (int i = b; i < nums.length; i++) { p.next = new ListNode(nums[i]); p = p.next;
if (loopPos == i) { loop = p; } }
if (loop != null) { p.next = loop; }
return head; }
public static ListNode genLinkedListWithHeadNode(int[] nums) { return genLinkedList(nums, true, -1); }
public static ListNode genLinkedListWithoutHeadNode(int[] nums) { return genLinkedList(nums, false, -1); }
public static ListNode genLoopedLinkedListWithHeadNode(int[] nums, int pos) { return genLinkedList(nums, true, pos); }
public static ListNode genLoopedLinkedListWithoutHeadNode(int[] nums, int pos) { return genLinkedList(nums, false, pos); }
public static int getCount(ListNode head, boolean withHead) { ListNode p = withHead ? head.next : head;
int c = 0; while (p != null) { c++; p = p.next; }
return c; }
public static int[] toArray(ListNode head, boolean withHead) { int c = getCount(head, withHead); int[] nums = new int[c];
ListNode p = withHead ? head.next : head;
int k = 0; while (p != null) { nums[k] = p.val;
k++; p = p.next; }
return nums; }
public static int[] toArrayWithHeadNode(ListNode head) { return toArray(head, true); }
public static int[] toArrayWithoutHeadNode(ListNode head) { return toArray(head, false); } }
public static class ArrayUtils { public static void printArray(int[] array) { System.out.println(java.util.Arrays.toString(array)); }
public static void printArray(int[] array, int len) { System.out.println(java.util.Arrays.toString(java.util.Arrays.copyOf(array, len))); }
public static void printArrays(int[][] arrays) { System.out.println(java.util.Arrays.deepToString(arrays)); }
public static void printArray(Integer[] array) { System.out.println(java.util.Arrays.toString(array)); }
public static void printArray(Integer[] array, int len) { System.out.println(java.util.Arrays.toString(java.util.Arrays.copyOf(array, len))); }
public static void printArrays(Integer[][] arrays) { System.out.println(java.util.Arrays.deepToString(arrays)); }
public static void printArray(String[] array) { System.out.println(java.util.Arrays.toString(array)); }
public static void printArray(String[] array, int len) { System.out.println(java.util.Arrays.toString(java.util.Arrays.copyOf(array, len))); }
public static void printArrays(String[][] arrays) { System.out.println(java.util.Arrays.deepToString(arrays)); } }
public static void main(String[] args) { testCase1(); testCase2(); testCase3(); }
public static void testCase1() { int[] nums1 = { 2, 4, 3 }; int[] nums2 = { 5, 6, 4 };
ListNode l1 = ListNode.genLinkedListWithoutHeadNode(nums1); ListNode l2 = ListNode.genLinkedListWithoutHeadNode(nums2); ListNode result = new Solution().addTwoNumbers(l1, l2); ArrayUtils.printArray(ListNode.toArrayWithoutHeadNode(result)); }
public static void testCase2() { int[] nums1 = { 0 }; int[] nums2 = { 0 };
ListNode l1 = ListNode.genLinkedListWithoutHeadNode(nums1); ListNode l2 = ListNode.genLinkedListWithoutHeadNode(nums2); ListNode result = new Solution().addTwoNumbers(l1, l2); ArrayUtils.printArray(ListNode.toArrayWithoutHeadNode(result)); }
public static void testCase3() { int[] nums1 = { 9, 9, 9, 9, 9, 9, 9 }; int[] nums2 = { 9, 9, 9, 9 };
ListNode l1 = ListNode.genLinkedListWithoutHeadNode(nums1); ListNode l2 = ListNode.genLinkedListWithoutHeadNode(nums2); ListNode result = new Solution().addTwoNumbers(l1, l2); ArrayUtils.printArray(ListNode.toArrayWithoutHeadNode(result)); } }
|