Don
Don

Reputation: 1572

Codes work in one environment but not another

This function is working in LeetCode's execution environment with correct output [1,1,2,3,4,4]

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode dummyNode = new ListNode(-1);
    ListNode l3 = dummyNode;

    while(l1!=null &&l2!=null){
        if(l1.val<l2.val){
            l3.next=l1;
            l1=l1.next;
        }
        else{
            l3.next=l2;
            l2=l2.next;               
        }

        l3=l3.next;    
    }

    l3.next=l1==null?l2:l1;

    return dummyNode.next;
}

I expanded to add other codes and called this function in Eclipse but they are not working. The problems are in

l3.next=l1;

and

l3.next=l2;

Source codes:

class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

public class MyLeetCode {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyNode = new ListNode(-1);
        ListNode l3 = dummyNode;
        
        while(l1!=null &&l2!=null){
            if(l1.val<l2.val){
                l3.next=l1;
                l1=l1.next;
            }
            else{
                l3.next=l2;
                l2=l2.next;               
            }
            
            l3=l3.next;    
        }
        
        l3.next=l1==null?l2:l1;
        
        return dummyNode.next;
    }
    public static void main(String[] args) {
        int[] A = {1,2,4};
        int[] B = {1,3,4};
        
        ListNode currA = null, currB=null;
        
        for(int i=A.length-1; i>=0; i--) {
            ListNode ls =new ListNode(A[i], currA);
            currA=ls;
        }

        for(int i=A.length-1; i>=0; i--) {
            ListNode ls=new ListNode(B[i], currB);
            currB=ls;
        }
        
        ListNode ret = mergeTwoLists(currA, currB);
        
        while(ret!=null)
            System.out.print(ret.val + " ");
        
    }

}

Upvotes: 0

Views: 51

Answers (1)

Thiyanesh
Thiyanesh

Reputation: 2360

Issue

The issue is in the

        while(ret!=null)
            System.out.print(ret.val + " ");
  • ret need to move forward
  • currently it will run infinitely (print only the first node value)

Correction

        while (ret! = null) {
            System.out.print(ret.val + " ");
            ret = ret.next;
        }

Full Soution

public class MyLeetCode {

    // individual node in the List
    static class ListNode {
        private final int val;
        private ListNode next;

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    protected static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(-1, null); // to reduce custom check for head
        ListNode current = dummyHead;

        // merge while both the lists have value
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }

            current = current.next;
        }

        // concatenate the list with remaining elements(if any)
        current.next = l1 == null ? l2 : l1;

        // return begin of the merged list
        return dummyHead.next;
    }

    // create list from the input array. preserves the order of nums
    protected static ListNode createList(int[] nums) {
        ListNode head = null;
        for (int i = nums.length - 1; i >= 0; i--) {
            ListNode ls = new ListNode(nums[i], head);
            head = ls;
        }
        return head;
    }

    // prints the list - starting from input node till end of list
    protected static void printList(ListNode node) {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ListNode headA = createList(new int[] {1, 2, 4});
        ListNode headB = createList(new int[] {1, 3, 4, 5});
        printList(headA);
        printList(headB);

        ListNode mergedHead = mergeTwoLists(headA, headB);
        printList(mergedHead);
    }
}

Upvotes: 1

Related Questions