Skip to content

Commit 1b3dee7

Browse files
committed
2074. Reverse Nodes in Even Length Groups: RETRY
1 parent 79fe2dd commit 1b3dee7

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,3 +1564,4 @@ mod s2069_walking_robot_simulation_ii;
15641564
mod s2070_most_beautiful_item_for_each_query;
15651565
mod s2071_maximum_number_of_tasks_you_can_assign;
15661566
mod s2073_time_needed_to_buy_tickets;
1567+
mod s2074_reverse_nodes_in_even_length_groups;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* [2074] Reverse Nodes in Even Length Groups
3+
*
4+
* You are given the head of a linked list.
5+
* The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,
6+
*
7+
* The 1^st node is assigned to the first group.
8+
* The 2^nd and the 3^rd nodes are assigned to the second group.
9+
* The 4^th, 5^th, and 6^th nodes are assigned to the third group, and so on.
10+
*
11+
* Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.
12+
* Reverse the nodes in each group with an even length, and return the head of the modified linked list.
13+
*
14+
* Example 1:
15+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/10/25/eg1.png" style="width: 699px; height: 124px;" />
16+
* Input: head = [5,2,6,3,9,1,7,3,8,4]
17+
* Output: [5,6,2,3,9,1,4,8,3,7]
18+
* Explanation:
19+
* - The length of the first group is 1, which is odd, hence no reversal occurs.
20+
* - The length of the second group is 2, which is even, hence the nodes are reversed.
21+
* - The length of the third group is 3, which is odd, hence no reversal occurs.
22+
* - The length of the last group is 4, which is even, hence the nodes are reversed.
23+
*
24+
* Example 2:
25+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/10/25/eg2.png" style="width: 284px; height: 114px;" />
26+
* Input: head = [1,1,0,6]
27+
* Output: [1,0,1,6]
28+
* Explanation:
29+
* - The length of the first group is 1. No reversal occurs.
30+
* - The length of the second group is 2. The nodes are reversed.
31+
* - The length of the last group is 1. No reversal occurs.
32+
*
33+
* Example 3:
34+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/11/17/ex3.png" style="width: 348px; height: 114px;" />
35+
* Input: head = [1,1,0,6,5]
36+
* Output: [1,0,1,5,6]
37+
* Explanation:
38+
* - The length of the first group is 1. No reversal occurs.
39+
* - The length of the second group is 2. The nodes are reversed.
40+
* - The length of the last group is 2. The nodes are reversed.
41+
*
42+
*
43+
* Constraints:
44+
*
45+
* The number of nodes in the list is in the range [1, 10^5].
46+
* 0 <= Node.val <= 10^5
47+
*
48+
*/
49+
pub struct Solution {}
50+
use crate::util::linked_list::{to_list, ListNode};
51+
52+
// problem: https://leetcode.com/problems/reverse-nodes-in-even-length-groups/
53+
// discuss: https://leetcode.com/problems/reverse-nodes-in-even-length-groups/discuss/?currentPage=1&orderBy=most_votes&query=
54+
55+
// submission codes start here
56+
57+
// Definition for singly-linked list.
58+
// #[derive(PartialEq, Eq, Clone, Debug)]
59+
// pub struct ListNode {
60+
// pub val: i32,
61+
// pub next: Option<Box<ListNode>>
62+
// }
63+
//
64+
// impl ListNode {
65+
// #[inline]
66+
// fn new(val: i32) -> Self {
67+
// ListNode {
68+
// next: None,
69+
// val
70+
// }
71+
// }
72+
// }
73+
impl Solution {
74+
pub fn reverse_even_length_groups(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
75+
Some(Box::new(ListNode::new(0)))
76+
}
77+
}
78+
79+
// submission codes end
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
85+
#[test]
86+
#[ignore]
87+
fn test_2074_example_1() {
88+
let head = linked![5, 2, 6, 3, 9, 1, 7, 3, 8, 4];
89+
90+
let result = linked![5, 6, 2, 3, 9, 1, 4, 8, 3, 7];
91+
92+
assert_eq!(Solution::reverse_even_length_groups(head), result);
93+
}
94+
95+
#[test]
96+
#[ignore]
97+
fn test_2074_example_2() {
98+
let head = linked![1, 1, 0, 6];
99+
100+
let result = linked![1, 0, 1, 6];
101+
102+
assert_eq!(Solution::reverse_even_length_groups(head), result);
103+
}
104+
105+
#[test]
106+
#[ignore]
107+
fn test_2074_example_3() {
108+
let head = linked![1, 1, 0, 6, 5];
109+
110+
let result = linked![1, 0, 1, 5, 6];
111+
112+
assert_eq!(Solution::reverse_even_length_groups(head), result);
113+
}
114+
}

0 commit comments

Comments
 (0)