> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/bu-chong-4060-dao/328.-odd-even-linked-list-jing-dian.md).

# 328. Odd Even Linked List(经典)

\# Medium

{% hint style="info" %}
非常锻炼**LinkList**基本功的一道题，要设置3个**head**，一个`odd`, 一个`even`，这两个指针都是动态的，还有一个evenHead，最后返回head，head一直不动

Key iead: `even` 的下一个是`odd`的下一个，`even` 的下下一个才是`even`的下一个
{% endhint %}

```python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        # edge case
        if head == None or head.next == None:
            return head

        odd = head
        even = head.next
        evenHead = even
        
        while even != None and even.next != None:
            odd.next = even.next
            odd = odd.next
            even.next = even.next.next
            even = even.next
        
        odd.next = evenHead
        return head
```

Time = $$O(n)$$ , Space = $$O(1)$$&#x20;

{% hint style="danger" %}
Python中初始化一个**Node**时，无需`odd=ListNode(None)`, `odd=head`, 而是直接 `odd=head`
{% endhint %}
