diff options
Diffstat (limited to '328')
-rw-r--r-- | 328/go.mod | 3 | ||||
-rw-r--r-- | 328/main.go | 52 |
2 files changed, 55 insertions, 0 deletions
diff --git a/328/go.mod b/328/go.mod new file mode 100644 index 0000000..a6abc32 --- /dev/null +++ b/328/go.mod @@ -0,0 +1,3 @@ +module 328 + +go 1.18 diff --git a/328/main.go b/328/main.go new file mode 100644 index 0000000..64f4a6b --- /dev/null +++ b/328/main.go @@ -0,0 +1,52 @@ +package main + +import "fmt" + +type ListNode struct { + Val int + Next *ListNode +} + +func dumpList(head *ListNode) { + for head.Next != nil { + fmt.Println(head.Val) + head = head.Next + } + fmt.Println(head.Val) + fmt.Println() +} + +func oddEvenList(head *ListNode) *ListNode { + if head == nil { + return nil + } + odd, even := head, head.Next + evenHead := even + for even != nil && even.Next != nil { + odd.Next = even.Next + odd = odd.Next + even.Next = odd.Next + even = even.Next + } + odd.Next = evenHead + return head +} + +func main() { + node1 := &ListNode{1, nil} + node2 := &ListNode{2, nil} + node3 := &ListNode{3, nil} + node4 := &ListNode{4, nil} + node5 := &ListNode{5, nil} + node1.Next = node2 + node2.Next = node3 + node3.Next = node4 + node4.Next = node5 + head := node1 + head = oddEvenList(node1) + for head.Next != nil { + fmt.Println(head.Val) + head = head.Next + } + fmt.Println(head.Val) +} |