/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ funcreverseKGroup(head *ListNode, k int) *ListNode { poi := &ListNode{Next: head} pre := poi for head != nil { tail := pre for i:=0; i<k; i++ { tail = tail.Next if tail == nil { return poi.Next } } next := tail.Next head, tail = reverse(head, tail) pre.Next = head tail.Next = next pre = tail head = tail.Next } return poi.Next } funcreverse(start, end *ListNode) (*ListNode, *ListNode) { pre := end.Next cur := start for pre != end { next := cur.Next cur.Next = pre pre = cur cur = next } return end, start }