-1

I tried writing a custom version of merge sort in which the sortList function is recursive but the merging function is iterative.

I have tried dry running but unable to figure out the problem.

This one is a custom testcase which is also resulting in Wrong Answer.

Your input:
5 4 3 1 2 6
Your function returned the following: 
1 -> 2 -> 3 -> 6 

Link of the question : https://www.interviewbit.com/problems/sort-list/

The entire code :

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/

ListNode* Solution::sortList(ListNode* A) {

    ListNode * head = A;

    if(!(head) || !(head->next))
    {   
        return head;
    }

    ListNode * slow = head, * fast = head;

    while((fast->next) && (fast->next->next))
    {
        fast = fast->next->next;
        slow = slow->next;
    }

    ListNode * temp = slow->next;

    slow->next = NULL;

    head = sortList(head);
    temp = sortList(temp);
    ListNode * ptr;
    ListNode * ans;

    ans = head->val > temp->val ? temp : head;

    while(head!=NULL && temp!=NULL)
    {
        if(head->val > temp->val){
            ptr = temp;
            temp = temp->next;
            ptr->next = head;
        }else
        {
            ptr = head;
            head = head -> next;
            ptr->next = temp;
        }
    }
    
    return ans;
}
At-U
  • 17
  • 6
  • 2
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Oct 05 '21 at 09:33
  • The best way to debug (and develop) pointer-oriented code is by drawing boxes and arrows on paper and trace through what happens and compare to what should happen. If I were you, I would do that and start with some three-element lists as test cases. – molbdnilo Oct 05 '21 at 09:52
  • Your merging loop only works when the two sublists are of equal length, i.e. when the input's length is even. For instance, if you sort `1 -> 2 -> 3`, you're going to merge `1 -> 2` and `3`, "lose" the 2, and end up with `1 -> 3`. – molbdnilo Oct 05 '21 at 10:14
  • Note that a [bottom up merge sort for linked list](https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists) can be significantly faster than top down. – rcgldr Oct 05 '21 at 16:42

1 Answers1

0

The merge function I was trying to implement was wrong.

Here is the correct code.

ListNode* Solution::sortList(ListNode* A) {

    ListNode * head = A;

    if(!(head) || !(head->next))
    {   
        return head;
    }

    ListNode * slow = head, * fast = head;

    while((fast->next) && (fast->next->next))
    {
        fast = fast->next->next;
        slow = slow->next;
    }

    ListNode * temp = slow->next;

    slow->next = NULL;

    head = sortList(head);
    temp = sortList(temp);

    ListNode * ans = new ListNode(0);

    ListNode * realAns = ans;

    while(head!=NULL && temp!=NULL)
    {
        if(head->val > temp->val)
        {
            ans->next = temp;
            temp = temp->next;
        }else{
            ans->next = head;
            head = head->next;
        }

        ans = ans->next;
    }
    
    if(temp==NULL && head!=NULL)
    {
        ans->next = head;
    }else if(temp!=NULL && head==NULL)
    {
        ans->next = temp;
    }

    return realAns->next;
}
At-U
  • 17
  • 6
  • Generally, code-only answers are not good answers. This also applies to when answering your own question. Therefore, you may want to explain in a few words what exactly was wrong and how you fixed it. – Andreas Wenzel Oct 05 '21 at 12:07