本文主要是介绍Golang | Leetcode Golang题解之第143题重排链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
func middleNode(head *ListNode) *ListNode {slow, fast := head, headfor fast.Next != nil && fast.Next.Next != nil {slow = slow.Nextfast = fast.Next.Next}return slow
}func reverseList(head *ListNode) *ListNode {var prev, cur *ListNode = nil, headfor cur != nil {nextTmp := cur.Nextcur.Next = prevprev = curcur = nextTmp}return prev
}func mergeList(l1, l2 *ListNode) {var l1Tmp, l2Tmp *ListNodefor l1 != nil && l2 != nil {l1Tmp = l1.Nextl2Tmp = l2.Nextl1.Next = l2l1 = l1Tmpl2.Next = l1l2 = l2Tmp}
}func reorderList(head *ListNode) {if head == nil {return}mid := middleNode(head)l1 := headl2 := mid.Nextmid.Next = nill2 = reverseList(l2)mergeList(l1, l2)
}
这篇关于Golang | Leetcode Golang题解之第143题重排链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!