链表中的节点每K个一组翻转
链表中的节点每k个一组翻转
https://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e?tpId=117&tags=&title=&diffculty=0&judgeStatus=0&rp=1
class Solution { public: /// 参考翻转pairs,翻转x~x+(k-1)之间的节点, x->next = reverseKGroup(x+k,k) ListNode* reverse(ListNode *first,ListNode *last) { ListNode *pre = nullptr; while(first!=last) { ListNode *temp = first->next; first->next = pre; pre = first; first = temp; } return pre; } ListNode *reverseKGroup(ListNode *head, int k) { if(!head) return nullptr; ListNode *node = head; for(int i=0;i<k;i++) { if(!node) return head; node = node->next; } ListNode *newHead = reverse(head,node); head->next = reverseKGroup(node,k); return newHead; } };