16-LINE SIMPLE C SOLUTION


  • 0
    Y
    struct ListNode* reverseKGroup(struct ListNode* head, int k) {
    	if (head == NULL || k <= 1) return head;
    
    	struct ListNode *lastNode = head;
    	struct ListNode *current = NULL;
    
    	for (int i = 0;i < k ;i++) {
    		if (lastNode == NULL) return reverseKGroup(current, i);
    		
    		struct ListNode *tail = lastNode;
    		lastNode = lastNode->next;
    		
    		tail->next = current;
    		current = tail;
    	}
    	
    	head->next = reverseKGroup(lastNode, k);
    	return current;
    }
    

Log in to reply
 

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