牛客题霸 NC2 重排链表
生活随笔
收集整理的這篇文章主要介紹了
牛客题霸 NC2 重排链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b
解決方案
Go
func reorderList(head *ListNode) {// write code heresolve(0, head)}func solve(i int, head *ListNode) (rj int, rtail *ListNode) {if head == nil {return -1, nil}j, tail := solve(i+1, head.Next)rj = j + 1if i == j {rtail = tail.Nexttail.Next = nilhead.Next = tail} else if i == j+1 {head.Next = nilrtail = tail} else if i < j {rtail = tail.Nexttail.Next = head.Nexthead.Next = tail} else {rtail = head}return }參考文章
總結
以上是生活随笔為你收集整理的牛客题霸 NC2 重排链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 279 完全平方数
- 下一篇: 牛客题霸 NC3 链表中环的入口结点