My 1ms C Solution


  • 0
    S

    struct ListNode *reverseBetween(struct ListNode *head, int m, int n) {

        if(m == n)
            return head ;
            
        int length = n - m + 1;
        
        struct ListNode** helper = (struct ListNode**)malloc(sizeof(struct ListNode*) * length);
        
        struct ListNode* mth , *nth ;
        mth = nth = head ;
        m--;
        n--;
        
        while(n)
        {
            
            if(m)
            {
                m--;
                mth = mth->next;
            }
            
            if(n)
            {
                n--;
                nth = nth->next;
            }
            
        }
        
        int i = 0;
        
        for(struct ListNode *t = mth ; t != nth ; t = t->next)
        {
            helper[i] = t ; 
            i++;
        }
        
        helper[i] = nth ;
        
        int temp ;
        int border = length / 2 ;
        
        for(i = 0 ; i <  border ;i++ )
        {
            temp = helper[i]->val;
            helper[i]->val =  helper[length - i - 1]->val;
            helper[length - i - 1]->val = temp ;
        }
        
        return head;
    

    }


Log in to reply
 

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