马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode node = new ListNode(0);
ListNode head = node;
while(l1 != null || l2 != null){
if(l2 == null || (l1 != null && l2.val >= l1.val)){
node.next = l1;
l1 = l1.next;
node = node.next;
}
else if (l1 == null || (l2 != null && l1.val >= l2.val)){
node.next = l2;
l2 = l2.next;
node = node.next;
}
}
return head.next;
}
}
|