Does anyone can tell me my solution's complexity?


  • 0
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode fakeHead = new ListNode(-1);
            fakeHead.next = l1;
            ListNode add = fakeHead;
            ListNode delete = l2;
            while(delete != null){
                
                while(add.next != null && add.next.val <= delete.val){
                    add = add.next;// find the position
                }
                
                ListNode temp = delete.next; // store new delete head
                delete.next = add.next;
                add.next = delete;
                add = delete;
                delete = temp;
            }
            return fakeHead.next;
        }

  • -1
    S

    Well, looks like it's O(n^2) since you have a nested while loop


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.