广州新塘建网站,腾讯会议新闻,网站安全怎么做,网络架构如何写25. K 个一组翻转链表
面试真的碰到了#xff0c;可惜没a出来。 没啥特殊的算法#xff0c;就是很难绕出来感觉。
/*** Definition for singly-linked list.* type ListNode struct {* Val int* Next *ListNode* }*/
func reverseKGroup(head *ListNode, k int) *L…25. K 个一组翻转链表
面试真的碰到了可惜没a出来。 没啥特殊的算法就是很难绕出来感觉。
/*** Definition for singly-linked list.* type ListNode struct {* Val int* Next *ListNode* }*/
func reverseKGroup(head *ListNode, k int) *ListNode {hair : ListNode{Next : head}pre : hairfor head ! nil{tail : prefor i : 0; i k; i{tail tail.Nextif tail nil{return hair.Next}}nex : tail.Nexthead, tail newchange(head, tail)pre.Next headtail.Next nexpre tailhead tail.Next}return hair.Next
}
func newchange(head, tail *ListNode)(*ListNode, *ListNode){prev : tail.Nextp : headfor prev ! tail{nex : p.Nextp.Next prevprev pp nex}return tail, head
}