leetcode 328. 奇偶链表

题解

分成奇链和偶链两条链,然后把奇链的尾指向偶链的头

示例代码(go)

1
2
3
4
5
6
7
8
9
10
11
12
13
func oddEvenList(head *ListNode) *ListNode {
if head == nil{
return nil
}
odd, even, evenHead := head, head.Next, head.Next
for even != nil && even.Next != nil{
odd.Next, even.Next = even.Next, even.Next.Next
odd = odd.Next
even = even.Next
}
odd.Next = evenHead
return head
}