A little bit longer code, but now we can do it in one pass in this recursive solution. I did declare a new pointer inside the recursive function, but it goes out of scope before the next recursion is called, so I don't think we use additional stack space. I also cleaned up my dummy pointer before returning the final result.

class Solution { private: void appendtail(ListNode* pre, ListNode* p, int l, int k) { for (int i=0; i<k-1; i++) { ListNode *next = p->next->next; p->next->next=pre->next; pre->next=p->next; p->next=next; } if (l>=k) appendtail(p,p->next,l-k,k); } public: ListNode* reverseKGroup(ListNode* head, int k) { if (k<2) return head; ListNode *dummy = new ListNode(0),*p=dummy; dummy->next=head; int l=0; while (p=p->next) l++; if (l>=k) appendtail(dummy,dummy->next,l-k,k); head=dummy->next; delete dummy; return head; } };Reverse Nodes in k-Group