0 ms solution in C :) :) :)


  • 1
    S

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

        if (!head || m-n==0)
            return head;
    
        struct ListNode *curr 	= head;
        struct ListNode *next 	= NULL;
        struct ListNode *prev 	= NULL;
        struct ListNode *start      = NULL;
        struct ListNode *end  	= NULL;
        struct ListNode *prevtmp 	= NULL;
    
        int pos=m;
        while(pos-1>0 && curr){
        	prevtmp=curr;
        	curr=curr->next;
        	pos--;
        }
    
        end=curr;
    
        while(curr && n-m+1>0){
    
        	next=curr->next;
        	curr->next=prev;
    
        	prev=curr;
        	curr=next ;
            start = prev;
        	m++;
        }
    
        end->next=next;//Tail
        if(prevtmp)
        	prevtmp->next=start;
        else
        	head=start;
    
    
        return head;
    

    }


  • 0
    C

    This is my c code

    struct ListNode* reverseBetween(struct ListNode* head, int m, int n)
    {
    struct ListNode *_prev = NULL, *_start = NULL, *_next = NULL, *_end = NULL, *node;
    struct ListNode dummy = (struct ListNode)malloc(sizeof(struct ListNode));
    int flg = 0;
    dummy->next = head;
    _prev = dummy;

    n -= m;
    
    while (--m) _prev = _prev->next;
    _start = _prev->next;
    _end = _start;
    
    flg = n+1;
    while(--flg) _end = _end->next;
    
    while (n--) 
    {
    	_next = _start->next;
    	node = _end->next;
    	_end->next = _start;
    	_start->next = node;
    	_start = _next;
    }
    
    _prev->next = _end;
    return dummy->next;
    

    }


Log in to reply
 

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