Answers for "revrese node in k group"

0

revrese node in k group

ListNode* reverseKGroup(ListNode* head, int k) {
        int count = 0;
        ListNode* prevptr= NULL; 
        ListNode* currptr= head;
        ListNode* nextptr;
        ListNode *check = head;
        
        while(check) {
            count++;
            check = check->next;
        }
        
        if(count < k) return currptr;
        else count = 0;
       
        while(currptr!=NULL && count<k){
            nextptr = currptr->next;
            currptr->next=prevptr;
            prevptr=currptr;
            currptr=nextptr;
            count ++;
        }
		
        if(nextptr!=NULL)
            head ->next = reverseKGroup(nextptr, k);
			
         return prevptr;
    }
Posted by: Guest on July-24-2021

Browse Popular Code Answers by Language