public ListNode reverseKGroup(ListNode head, int k) {

        if (k <= 1 || head == null || head.getNext() == null) {
            return head;
        }

        ListNode prev = new ListNode(0);
        prev.setNext(head);
        head = prev;

        //find current position
        ListNode cur = prev.getNext();
        //move k steps
        while (cur != null) {
            int counter = k;
            while (cur != null && counter > 1) {
                cur = cur.getNext();
                counter--;
            }
            //if not reach the end
            if (cur != null) {
                cur = prev.getNext();
                counter = k;
                //switch 
                while (counter > 1) {
                    ListNode temp = cur.getNext();
                    cur.setNext(temp.getNext());
                    temp.setNext(prev.getNext());
                    prev.setNext(temp);
                    counter--;
                }
                prev = cur;
                cur = prev.getNext();
            }
        }

        return head.getNext();
    }