解题思路:
1、对输出链表向尾向头赋值,引入一个临时变量next,尾部时next=None, 后续next=cur
'''
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
# @param head ListNode类 
# @param k int整型 
# @return ListNode类
#
class Solution:
    def reverseKGroup(self , head , k ):
        # write code here

        buf = []
        while head:
            buf.append(head.val)
            head = head.next

        n = len(buf)
        if n==0:
            return None

        i_max = n//k
        for i in range(i_max):
            t = buf[i*k:(i+1)*k]
            buf[i*k:(i+1)*k] = t[::-1]
        buf = buf[::-1]
        #print('buf=',buf)

        next = None
        for i in range(n):
            cur = ListNode(buf[i])
            cur.next = next
            next = cur

        return cur

head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
head.next.next.next.next = ListNode(5)
Solution().reverseKGroup(head,2)