diff --git a/code_kata_230828/reorder_list_test.go b/code_kata_230828/reorder_list_test.go new file mode 100644 index 0000000..400eef9 --- /dev/null +++ b/code_kata_230828/reorder_list_test.go @@ -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 +}