Concise C++ Solution


  • 0
    M
    class Solution {
    public:
    	ListNode* reverseKGroup(ListNode* head, int k) {
    		ListNode *p = head,*temp,*pre,*succ;
    		for(int i=0;i<k;i++)
    		{
    			if (p == nullptr) return head;
    			p = p->next;
    		}
    		temp = head->next;
    		head->next = reverseKGroup(p,k);
    		pre = head;
    		for (int i = 1; i < k; i++)
    		{
    			succ = temp->next;
    			temp->next = pre;
    			pre = temp;
    			temp = succ;
    		}
    		return pre;
    	}
    };
    

Log in to reply
 

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