@Test public void test() { final ListNode l1 = ListNode.create(new int[]{1, 3, 8, 11, 15}); log.debug(l1.dump()); final ListNode l2 = ListNode.create(new int[]{2}); log.debug(l2.dump()); final ListNode l3 = inst.mergeTwoLists(l1, l2); log.debug(l3.dump()); }
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = null; ListNode tail = null; ListNode hdr1 = l1; ListNode hdr2 = l2; for (; !(hdr1 == null || hdr2 == null);) { ListNode node; if (hdr1.val > hdr2.val) { node = hdr2; hdr2 = hdr2.next; } else { node = hdr1; hdr1 = hdr1.next; } node.next = null; if (tail == null) { result = node; tail = node; } else { tail.next = node; tail = node; } } tail.next = (hdr1 == null ? hdr2 : hdr1); return result; }
MergeTwoSortedLists { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = null; ListNode tail = null; ListNode hdr1 = l1; ListNode hdr2 = l2; for (; !(hdr1 == null || hdr2 == null);) { ListNode node; if (hdr1.val > hdr2.val) { node = hdr2; hdr2 = hdr2.next; } else { node = hdr1; hdr1 = hdr1.next; } node.next = null; if (tail == null) { result = node; tail = node; } else { tail.next = node; tail = node; } } tail.next = (hdr1 == null ? hdr2 : hdr1); return result; } }
MergeTwoSortedLists { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = null; ListNode tail = null; ListNode hdr1 = l1; ListNode hdr2 = l2; for (; !(hdr1 == null || hdr2 == null);) { ListNode node; if (hdr1.val > hdr2.val) { node = hdr2; hdr2 = hdr2.next; } else { node = hdr1; hdr1 = hdr1.next; } node.next = null; if (tail == null) { result = node; tail = node; } else { tail.next = node; tail = node; } } tail.next = (hdr1 == null ? hdr2 : hdr1); return result; }  }
MergeTwoSortedLists { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = null; ListNode tail = null; ListNode hdr1 = l1; ListNode hdr2 = l2; for (; !(hdr1 == null || hdr2 == null);) { ListNode node; if (hdr1.val > hdr2.val) { node = hdr2; hdr2 = hdr2.next; } else { node = hdr1; hdr1 = hdr1.next; } node.next = null; if (tail == null) { result = node; tail = node; } else { tail.next = node; tail = node; } } tail.next = (hdr1 == null ? hdr2 : hdr1); return result; }  ListNode mergeTwoLists(ListNode l1, ListNode l2); }
MergeTwoSortedLists { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode result = null; ListNode tail = null; ListNode hdr1 = l1; ListNode hdr2 = l2; for (; !(hdr1 == null || hdr2 == null);) { ListNode node; if (hdr1.val > hdr2.val) { node = hdr2; hdr2 = hdr2.next; } else { node = hdr1; hdr1 = hdr1.next; } node.next = null; if (tail == null) { result = node; tail = node; } else { tail.next = node; tail = node; } } tail.next = (hdr1 == null ? hdr2 : hdr1); return result; }  ListNode mergeTwoLists(ListNode l1, ListNode l2);  }