/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode fakeHead = new ListNode(-1);
ListNode run = fakeHead;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
run.next = l1;
l1 = l1.next;
}else{
run.next = l2;
l2 = l2.next;
}
run = run.next;
}
run.next = l1==null ? l2 : l1;
return fakeHead.next;
}
}
Merge Two Sorted Lists
最新推荐文章于 2024-01-12 14:30:52 发布