Input two monotonically increasing lists, output the combined list of two lists, of course, we need the combined list to meet the monotonic rule. // No need to new the node, just compare the values and point to the next node.

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null && list2==null){
            return null;
        }
        
        //
        ListNode p1 = new ListNode(0);
        ListNode head=p1;
        while(list1! =null && list2! =null){if(list1.val>list2.val){
                p1.next=list2;
                list2=list2.next;
            }else{
                p1.next=list1;
                list1=list1.next;
            }
            p1=p1.next;
        }
        if(list1! =null){ p1.next=list1; }if(list2! =null){ p1.next=list2; }returnhead.next; }}Copy the code