/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode l3=null,begin=null;
while(l1!=null || l2!=null)
{
int now;
if(l1!=null && l2!=null)
{
if(l1.val<=l2.val){
now=l1.val;
l1=l1.next;
}
else{
now=l2.val;
l2=l2.next;
}
}
else if(l1!=null)
{
now=l1.val;
l1=l1.next;
}
else
{
now=l2.val;
l2=l2.next;
}
if(l3==null)
{
l3=new ListNode(now);
begin=l3;
}
else
{
l3.next=new ListNode(now);
l3=l3.next;
}
}
return begin;
}
}
Leetcode t21 - java
Guess you like
Origin blog.csdn.net/qq_41563270/article/details/109457594
Recommended
Ranking