go: add «328. Odd Even Linked List»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-15 20:23:28 +02:00
parent f6ac06990e
commit a347dcfcd6
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,17 @@
package main
func oddEvenList(head *ListNode) *ListNode {
dummies := []ListNode{ListNode{Val: 0, Next: nil}, ListNode{Val: 0, Next: nil}}
tails := []*ListNode{&dummies[0], &dummies[1]}
for idx := 0; head != nil; idx++ {
tails[idx%2].Next = head
tails[idx%2] = head
head = head.Next
}
tails[0].Next = dummies[1].Next
tails[1].Next = nil
return dummies[0].Next
}