# leetcode21 Merge Two Sorted Lists 将两个有序链表组合成一个新的有序链表

## 题目要求

``````Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
``````

## 思路一：循环

``````public class MergeTwoSortedLists_21 {

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode start = new ListNode(0);
ListNode temp = new ListNode(0);
start.next = temp;
while(l1!=null && l2!=null){
if(l1.val <= l2.val){
temp.next = l1;
l1 = l1.next;
}else{
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
if(l1!=null){
temp.next = l1;
}
if(l2!=null){
temp.next = l2;
}
return start.next.next;
}

public class ListNode{
int val;
ListNode next;
ListNode(int x) { val = x; }
}
}``````

## 思路二：递归

``````public class MergeTwoSortedLists_21 {
public ListNode mergeTwoLists_recursive(ListNode l1, ListNode l2){
if(l1==null){
return l2;
}else if (l2==null){
return l1;
}
if(l1.val <= l2.val){
}else{
}
}

public class ListNode{
int val;
ListNode next;
ListNode(int x) { val = x; }
}
}
``````