Software engineer

LC-Merge Two Sorted Lists

LC-Merge Two Sorted Lists

Merge Two Sorted Lists

Question

You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list.
The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.

Input: list1 = [1,2,4], list2 = [1,3,4]

Output: [1,1,2,3,4,4]

Input: list1 = [], list2 = [] Output: []

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1!=null && list2!=null){

            if(list1.val<list2.val){//
                list1.next=mergeTwoLists(list1.next, list2);
                return list1;
            }else{
                list2.next=mergeTwoLists(list2.next, list1);
                return list2;
            }
            }
        if(list1==null){
            return list2;
        }return list1;
        }
    }
comments powered by Disqus