1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode res = new ListNode(); ListNode tail = res; while (l1 != null || l2 != null) { if (l2 == null || (l1 != null && l1.val < l2.val)) { tail.next = l1; tail = l1; l1 = l1.next; }else { tail.next = l2; tail = l2; l2 = l2.next; } } return res.next; } |
Direct link: https://paste.plurk.com/show/PPb9JSXg5NnEnJvC11lf