题目描述
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
返回dum.next;
代码:
/**
* 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 newnode = new ListNode(-1);
ListNode cur = newnode;
while(l1 != null && l2 != null ){
if(l1.val < l2.val){
newnode.next = l1;
l1 = l1.next;
}else{
newnode.next = l2;
l2 = l2.next;
}
newnode = newnode.next;
}
if(l1 != null){
newnode.next = l1;
}else{
newnode.next = l2;
}
return cur.next;
}
}