# Odd Even Linked List

{% embed url="<https://leetcode.com/problems/odd-even-linked-list/>" %}

## Solution 1

```go
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func oddEvenList(head *ListNode) *ListNode {
    oddHead, evenHead := &ListNode{Val: 0}, &ListNode{Val: 0}
    oddRunner, evenRunner, index := oddHead, evenHead, 1
    for head != nil {
        if index % 2 != 0 {
            oddRunner.Next = head
            oddRunner = oddRunner.Next
        } else {
            evenRunner.Next = head
            evenRunner = evenRunner.Next
        }
        index++
        head = head.Next
    }
    oddRunner.Next = evenHead.Next
    evenRunner.Next = nil
    return oddHead.Next
}
```

## Solution 2

```go
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
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
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/linked-list/odd-even-linked-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
