牛客题霸 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
}

参考文章


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部