/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p = l1;
ListNode q = l2;
ListNode head = new ListNode(0);
ListNode l = head;
if(l1 == null)return l2;
if(l2 == null)return l1;
while(p != null && (q != null))
{
if(p.val < q.val)
{
l.next= p;
l = l.next;
p = p.next;
}
else
{
l.next = q;
l = l.next;
q = q.next;
}
}
if(p == null)l.next = q;
else l.next = p;
return head.next;
}
}
leetcode--merge two sorted lists(java)
最新推荐文章于 2024-07-14 11:23:58 发布