# 328. Odd Even Linked List

## 題目敘述

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.

Example 1: ``````Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
``````

Example 2: ``````Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]
``````

Constraints:

• The number of nodes in the linked list is in the range `[0, 10**4]`.
• `-10**6 <= Node.val <= 10**6`

## 解法解析

### 解法範例

#### Go

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
return nil
}

for even != nil && even.Next != nil {
odd.Next = even.Next
odd = odd.Next
even.Next = odd.Next
even = even.Next
}
}
``````

#### JavaScript

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
var oddEvenList = function (head) {
return null;
}

while (even && even.next) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
};
``````

#### Kotlin

``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {
if (head == null) return null

while (even != null && even.next != null) {
odd!!.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
}
}
}
``````

#### PHP

``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @return ListNode
*/
{
return null;
}

while (!is_null(\$even) && !is_null(\$even->next)) {
\$odd->next = \$even->next;
\$odd = \$odd->next;
\$even->next = \$odd->next;
\$even = \$even->next;
}
}
}
``````

#### 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 | None) -> ListNode | None:
return None

while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
``````

#### Rust

``````
``````

#### Swift

``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func oddEvenList(_ head: ListNode?) -> ListNode? {
return nil
}

while even != nil && even?.next != nil {
odd?.next = even?.next
odd = odd?.next
even?.next = odd?.next
even = even?.next
}