Sword Point offer: Merge two sorted list "25" title description
Input two monotonically increasing list, output two list of linked lists, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.
Problem analysis
Each time is a comparison arrow node, the small node connected to the linked list has been merged, repeat the process, and finally if the list has already gone, then connect the other linked list directly to the merged list.
Java
public static ListNode Merge (ListNode list1,listnode list2) { if (list1==null) return list2; else if (list2==null) return list1; ListNode mergehead = null; if (list1.val<list2.val) { mergehead = List1; Mergehead.next = Merge (LIST1.NEXT,LIST2); } else { mergehead = List2; Mergehead.next = Merge (List1,list2.next); } return mergehead; }
Sword Point offer: Merge two sorted list "25"