From 24a587f95e3e35a06807f3dbac5318344bde58a2 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Tue, 6 Dec 2022 11:51:56 +0330 Subject: 328 --- 328/go.mod | 3 +++ 328/main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 328/go.mod create mode 100644 328/main.go (limited to '328') 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) +} -- cgit v1.2.3