# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        dummy=jump=ListNode(0)
        dummy.next=head
        r=head
        l=head
        while True:
            count=0
            while r and count<k:
                r=r.next
                count+=1
            if count==k:
                pre=r
                cur=l
                for _ in range(k):
                    cur.next,cur,pre=pre,cur.next,cur
                jump.next,jump,l=pre,l,r
            else:
                return dummy.next