@@ -0,0 +1,1295 @@
+/**%0A *%09Merge two sorted linked lists and return it as a new list.%0A *  The new list should be made by splicing together the nodes of the first two lists.%0A *%0A *%09Author: matao%0A */%0A%0Aclass ListNode %7B%0A%09int val;%0A%09ListNode next;%0A%09ListNode(int x) %7B val = x; %7D%0A%7D%0A%0A%0Apublic class MergeTwoSortedLists %7B%0A%09public static ListNode mergeTwoLists(ListNode l1, ListNode l2) %7B%0A%09%09ListNode dummy = new ListNode(0);%0A%09%09ListNode p = dummy;%0A%09%09while(l1 != null && l2 != null) %7B%0A%09%09%09if(l1.val %3C l2.val) %7B%0A%09%09%09%09p.next = l1;%0A%09%09%09%09l1 = l1.next;%0A%09%09%09%09p = p.next;%0A%09%09%09%7D else %7B%0A%09%09%09%09p.next = l2;%0A%09%09%09%09l2 = l2.next;%0A%09%09%09%09p = p.next;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if(l1 != null) %7B%0A%09%09%09p.next = l1;%0A%09%09%7D%0A%09%09if(l2 != null) %7B%0A%09%09%09p.next = l2;%0A%09%09%7D%0A%09%09return dummy.next;%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09ListNode head1 = new ListNode(0);%0A%09%09ListNode tail1 = head1;%0A%09%09for (int i = 0; i %3C 5; i++) %7B%0A%09%09%09ListNode newNode = new ListNode(i);%0A%09%09%09tail1.next = newNode;%0A%09%09%09tail1 = tail1.next;%0A%09%09%7D%0A%0A%09%09ListNode head2 = new ListNode(0);%0A%09%09ListNode tail2 = head2;%0A%09%09for (int i = 3; i %3C 6; i++) %7B%0A%09%09%09ListNode newNode = new ListNode(i);%0A%09%09%09tail2.next = newNode;%0A%09%09%09tail2 = tail2.next;%0A%09%09%7D%0A%09%09ListNode result = mergeTwoLists(head1.next, head2.next);%0A%09%09while(result != null) %7B%0A%09%09%09System.out.print(result.val + %22 %22);%0A%09%09%09result = result.next;%0A%09%09%7D%0A%09%09System.out.println();%0A%09%7D%0A%7D

add Merge Two Sorted Lists solution