题目描述
解题思路
- 遍历l1,l2比较当前节点值得的大小;
- 将较小值的节点添加至新链表中;
- 将对应链表的节点向后移一位。
代码实现
/**
* 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) {
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode l3 = new ListNode(0);
ListNode res = l3;
while(l1 != null || l2 != null){
if(l1 == null){
l3.next = l2;
break;
}
if(l2 == null){
l3.next = l1;
break;
}
if(l1.val > l2.val){
l3.next = l2;
l2 = l2.next;
}else{
l3.next = l1;
l1 = l1.next;
}
l3 = l3.next;
}
return res.next;
}
}