/*
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 list2;
        }

        if (list1 != null && list2 == null) {
            return list1;   
        }

        ListNode head = new ListNode(-1);
        head.next = null;
        ListNode tempHead = head;
        ListNode node1 = list1;
        ListNode node2 = list2;

        while (node1 != null && node2 != null) {

            if (node1.val < node2.val) {
                tempHead.next = node1;
                tempHead = node1;
                node1 = node1.next;
            } else {
                tempHead.next = node2;
                tempHead = node2;
                node2 = node2.next;
            }  
        }

        if (node1 != null) {
            tempHead.next = node1;
        }

        if (node2 != null) {
            tempHead.next = node2;
        }

        return head.next;
    }
}