带头结点
import java.util.* ;
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0) ;
ListNode now = head ;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
now.next = new ListNode(l1.val) ;
l1 = l1.next ;
}
else{
now.next = new ListNode(l2.val) ;
l2 = l2.next ;
}
now = now.next ;
}
while(l1 != null){
now.next = new ListNode(l1.val) ;
l1 = l1.next ;
now = now.next ;
}
while(l2 != null){
now.next = new ListNode(l2.val) ;
l2 = l2.next ;
now = now.next ;
}
return head.next ;
}
public static void main(String[] args){
}
}