code kata 230828

This commit is contained in:
VicRen
2023-08-28 10:32:17 +08:00
parent 0354f1103b
commit 45f6b47eba

View File

@@ -0,0 +1,73 @@
package main
import (
"fmt"
"testing"
)
func ReorderList(head *node) {
printList(head)
f, s := head, head
for f.next != nil && f.next.next != nil {
f = f.next.next
s = s.next
}
sh := s.next
s.next = nil
printList(head)
printList(sh)
var pre *node
for sh != nil {
next := sh.next
sh.next = pre
pre = sh
sh = next
}
printList(pre)
p1, p2 := head, pre
for p2 != nil {
next1, next2 := p1.next, p2.next
p1.next = p2
p2.next = next1
p1, p2 = next1, next2
}
printList(head)
}
func printList(n *node) {
for n != nil {
fmt.Printf("%d->", n.value)
n = n.next
}
fmt.Println("nil")
}
func TestReorderList(t *testing.T) {
ReorderList(makeList(31))
}
func makeList(count int) *node {
var h *node
var p *node
for i := 0; i < count; i++ {
n := &node{
value: i,
}
if h == nil {
h = n
p = h
} else {
p.next = n
p = n
}
}
return h
}
type node struct {
value int
next *node
}