private ListNode merge(ListNode head1, ListNode head2){
        ListNode head = new ListNode(0);
        ListNode p = head;
        ListNode temp = null;
        while (head1!=null && head2!=null){
            if(head2.val < head1.val){
               p.next=head2;
               if(head2.next != null){
                   temp = head2.next;
                   p.next.next = null;
                   head2 = temp;
               }else {
                   head2 = null;
               }

            }else {
                p.next=head1;
                if(head1.next != null){
                    temp = head1.next;
                    p.next.next = null;
                    head1 = temp;
                }else {
                    head1 = null;
                }

            }
            p=p.next;
        }
        if(head1!=null){
            p.next = head1;
        }
        if(head2!=null){
            p.next = head2;
        }
        return  head.next;
    }