# [Leetcode]328: Odd Even Linked List

Problem : Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.The first node is considered odd, and the second node is even, and so on.

Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in `O(1)` extra space complexity and `O(n)` time complexity.

Thoughts :

Solution :

`class Solution:    def oddEvenList(self, head: ListNode) -> ListNode:        if not head:            return None        odd, even = head, head.next        eventmp = even        while even and even.next:            # 1接到3            odd.next = even.next            # 1指標移到3            odd = odd.next            # 2接到4            even.next = odd.next             # 2指標移到4            even = even.next         # 1的最後一個指標接到2        odd.next = eventmp        return head`

Time Complexity : O(n)
# one while loop -> n

Space Complexity : O(1)

--

--