题目链接
思路是让while循环,一个指针记录节点,一个指针指向下一个节点即可
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyhead=new ListNode(0);
ListNode cur=dummyhead;
while(l1!=null&&l2!=null)
{
if(l1.val<l2.val)
{cur.next=l1; cur=cur.next; l1 =l1.next;
}
else
{cur.next=l2; cur=cur.next; l2=l2.next; }
}
if(l1==null)
{cur.next=l2;}
else
{ cur.next =l1; }
return dummyhead.next;
}
}
![](https://img-blog.csdnimg.cn/20200315213520528.png)