25. Reverse Nodes in k-Group

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head==null||k==1)
            return head;
        ListNode preNode=new ListNode(0);
        preNode.next=head;
        ListNode pre=preNode, cur=pre, nex;
        int num=0;
        while(cur.next!=null)
        {
            num++;
            cur=cur.next;
        }
        while(num>=k)
        {
            cur=pre.next;
            nex=cur.next;
            for(int i=1;i<k;i++)
            {
                cur.next=nex.next;
                nex.next=pre.next;
                pre.next=nex;
                nex=cur.next;
            }
            pre=cur;
            num-=k;
        }
        return preNode.next;
    }
}
原文地址:https://www.cnblogs.com/asuran/p/7580055.html