标签:
public class Solution { // 非递归 public ListNode Merge2(ListNode list1,ListNode list2) { ListNode dummy = new ListNode(0); ListNode lastNode = dummy; while(list1!=null && list2!=null){ if(list1.val<list2.val){ lastNode.next = list1; list1 = list1.next; }else{ lastNode.next = list2; list2 = list2.next; } lastNode = lastNode.next; } if(list1!=null){ lastNode.next = list1; }else{ lastNode.next = list2; } return dummy.next; } //递归 public ListNode Merge(ListNode list1,ListNode list2) { if(list1==null) return list2; 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; } }
标签:
原文地址:http://www.cnblogs.com/hesier/p/5584823.html