1. 跟上一道区间反转链表很相似,但那是这个起始位置是固定的,所以这部分处理会更简单一些;
  2. 思路就是循环k-1次,翻转一段链表,下一次从上一段链表的末尾开始,再来一次反转,计算一共需要反转count/k次。

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param k int整型 
 * @return ListNode类
 */
#include <stdlib.h>
struct ListNode* reverseKGroup(struct ListNode* head, int k ) {
    // write code here
    struct ListNode* real_head = (struct ListNode*)malloc(sizeof(struct ListNode));
    real_head->val = 0;
    real_head->next = head;
    struct ListNode* cur = real_head->next;
    struct ListNode* start = real_head;
    struct ListNode* temp = start->next;
    struct ListNode* head_count = head;
    int i = 0,j = 0,count = 0;
    while(head_count!=NULL)
    {
        head_count = head_count->next;
        count++;
    }
    for(j = 0;j<count/k;j++)
    {
        for(i = 0;i<k-1;i++)
        {
            start->next = cur->next;
            cur->next = cur->next->next;
            start->next->next = temp;
            temp = start->next; 
        }
        start = cur;
        cur = start->next;
        temp = start->next;
    }
    return real_head->next;
}