package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @param k int整型 
  * @return ListNode类
*/

func reverseNode(a, b *ListNode) *ListNode{
	var pre,cur,next *ListNode
	pre = nil
	cur = a
	next = a
	for cur != b {
		next = cur.Next
		cur.Next = pre
		pre = cur
		cur = next
	}
	return pre
}

func reverseKGroup(head *ListNode, k int) *ListNode {
	if head == nil || k == 1{
		return head
	}
    
	a,b := head,head
	for i:=0 ; i< k ;i++{
		if b == nil { //如果不足K个 直接返回
			return head
		}
		b = b.Next
	}

	newHead := reverseNode(a,b)
	a.Next = reverseKGroup(b,k)
	return newHead
}