328. Odd Even Linked List Medium
1/**
2 * [328] Odd Even Linked List
3 *
4 * 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.
5 * The first node is considered odd, and the second node is even, and so on.
6 * Note that the relative order inside both the even and odd groups should remain as it was in the input.
7 * You must solve the problem in O(1) extra space complexity and O(n) time complexity.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/oddeven-linked-list.jpg" style="width: 300px; height: 123px;" />
11 * Input: head = [1,2,3,4,5]
12 * Output: [1,3,5,2,4]
13 *
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/oddeven2-linked-list.jpg" style="width: 500px; height: 142px;" />
16 * Input: head = [2,1,3,5,6,4,7]
17 * Output: [2,3,6,7,1,5,4]
18 *
19 *
20 * Constraints:
21 *
22 * The number of nodes in the linked list is in the range [0, 10^4].
23 * -10^6 <= Node.val <= 10^6
24 *
25 */
26pub struct Solution {}
27use crate::util::linked_list::{ListNode, to_list};
28
29// problem: https://leetcode.com/problems/odd-even-linked-list/
30// discuss: https://leetcode.com/problems/odd-even-linked-list/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for singly-linked list.
35// #[derive(PartialEq, Eq, Clone, Debug)]
36// pub struct ListNode {
37// pub val: i32,
38// pub next: Option<Box<ListNode>>
39// }
40//
41// impl ListNode {
42// #[inline]
43// fn new(val: i32) -> Self {
44// ListNode {
45// next: None,
46// val
47// }
48// }
49// }
50impl Solution {
51 pub fn odd_even_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
52 Some(Box::new(ListNode::new(0)))
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_328() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.