aboutsummaryrefslogtreecommitdiffstats
path: root/328/main.go
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-06 08:21:56 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-06 08:21:56 +0000
commit24a587f95e3e35a06807f3dbac5318344bde58a2 (patch)
treee794e699c598d0452858bfbdf87efcd8749a90e1 /328/main.go
parent17 (diff)
downloadleetcode-24a587f95e3e35a06807f3dbac5318344bde58a2.tar.gz
leetcode-24a587f95e3e35a06807f3dbac5318344bde58a2.zip
328
Diffstat (limited to '')
-rw-r--r--328/main.go52
1 files changed, 52 insertions, 0 deletions
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)
+}