遍历链表,遇到k的整数倍节点时,翻转之前的节点。
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        if(!head || !head->next || k==1) return head;
        ListNode *h=(ListNode*)malloc(sizeof(ListNode));
        h->next=head;
        ListNode *p=h,*q=head;//p永远指向前一组已翻转节点的尾节点,q永远指向后一组将要翻转节点的首节点
        int n=0;
        while(q){
            n++;
            q=q->next;
            if(n%k==0){
                ListNode *r,*s1,*s2,*s3;//r用来记录下一个p值,s1,s2,s3用于翻转节点
                r=p->next;
                s1=r;
                s2=r->next;
                s3=s2->next;
                while(s3!=q){
                    s2->next=s1;
                    s1=s2;
                    s2=s3;
                    s3=s3->next;
                }
                s2->next=s1;
                p->next=s2;
                p=r;//更新p指针
                p->next=q;
            }
        }
        return h->next;
    }
};