why the split function is not returning to the previous call?

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




struct ListNode* midList(struct ListNode * start )
 {
     struct ListNode *slow =start ;
     struct ListNode *fast= start->next  ;
     
     while(fast!=nullptr)
     {
        slow=slow->next;
        fast=fast->next->next; 
     }
     return slow ;
     
     
 }
 

void split(struct ListNode *head)
 {   
     
     cout<<"head is "<<head->val<<endl;
     
     
     
     
     
     if(head->next==nullptr)
        return; 
    
    
    struct ListNode *mid=midList(head);
    
    cout<<"mid is "<<mid->val<<endl;
    
    
    struct ListNode *left =  new ListNode(0);
    struct ListNode *leftside=left,*pre=left;
    
    struct ListNode *temp=head;
    
    while(temp!=mid->next)
    {   struct ListNode *xyz =  new ListNode(0);
        xyz->val=temp->val;
        pre->next=xyz;
        pre=xyz;
        temp=temp->next;
    }
    
    struct ListNode *right =  new ListNode(0);
    struct ListNode *rightside=right,*pre1=right;
    
   // struct ListNode *temp=head;
    
    while(temp!=nullptr)
    {
        struct ListNode *right =  new ListNode(0);
        right->val=temp->val;
        pre1->next=right;
        pre1=right;
        temp=temp->next;
    }
    
    
    cout<<"printing the leftside "<<endl;
    struct ListNode *x=leftside ;
    x=x->next;
    while(x!=nullptr)
    {
        cout<<x->val<<" ";
        x=x->next;
        
    }
    cout<<endl;
    
    
     cout<<"printing the rightside "<<endl;
    struct ListNode *y=rightside ;
    y=y->next;
    while(y!=nullptr)
    {
        cout<<y->val<<" ";
        y=y->next;
        
    }
    cout<<endl;
    
    
    split(leftside->next);
    split(rightside->next);
    
    
    
    
    
 }
 




ListNode* Solution::sortList(ListNode* A) {
    
   split(A); 
    
    return A ;
}









input 
A : [ 3 -> 4 -> 2 -> 5 -> 1 ]
output 
    
    head is 3
    mid is 2
    printing the leftside 
    3 4 2 
    printing the rightside 
    5 1 
    head is 3
    mid is 4
    printing the leftside 
    3 4 
    printing the rightside 
    2 
    head is 3
    
    

This code is about sorting the linklist using the merge sort . I have applied the splitting functionality but its not working properly . I think after printing "head is 3" it is not returning to the function where it is called or there may be some other issue. I am new to recursion please help me to solve this issue .

Source: Windows Questions C++

LEAVE A COMMENT