leetcode 21. Merge Two Sorted Lists
/**
* 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;
}
}
页:
[1]