```
class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def reverseKGroup(self, head, k):
if head==None or head.next==None or k<=1:
return head
previous=ListNode(0)
previous.next=head
cur=head
count=0
while cur!=None: #count: how many elements in list
count+=1
cur=cur.next
if count<k: #check first
return head
numK=count/k #numK: times of reverse
i=0
pre=previous
cur=head
while i<numK: #loop numK times
j=1
while j<k: #each loop: reverse k nodes(like insertion)
temp=cur.next
cur.next=temp.next
temp.next=pre.next
pre.next=temp
j+=1
pre=cur
cur=cur.next
i+=1
return previous.next`enter code here`
```

The main idea: *count* the number of elements in the linked list first; compare *count* with *k*, if less than *k*, return head; then compute *numK* ,times of *k* reverse. Then loop *numK* times and in each loop, reverse *k* nodes. The total complexity is O(n) time complexity and O(1) space complexity