题解 | #链表中的节点每k个一组翻转#
链表中的节点每k个一组翻转
https://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param k int整型 * @return ListNode类 */ func reverseKGroup( head *ListNode , k int ) *ListNode { tail := head for i := 0; i < k; i++ { if tail == nil { return head } tail = tail.Next } cur := head var pre *ListNode for i := 0 ;;i++ { if cur == tail { break } temp := cur.Next cur.Next = pre pre = cur cur = temp } head.Next = reverseKGroup(tail, k) return pre }