Enter two incrementally sorted lists and merge them so that the nodes in the new list are still incrementally sorted.

Example 1: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4

public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode curNode=new ListNode(0); ListNode headNode=curNode; ListNode headNode=curNode; ListNode headNode=curNode; // loop through l1 and l2 while (l1! = null && l2 ! If (l1.val <= l2.val) {next = l1; if (l1.val <= l2.val) { // l1 = l1.next; } else {// If l2 is less than l1,l2 assigns the current value of the new list curnode. next = l2; // next = l2.next; } curNode = curNode.next; Curnode. next = (l1 == null? l2 : l1); return headNode.next; }}Copy the code