92. Reverse Linked List II Medium
1/**
2 * [92] Reverse Linked List II
3 *
4 * Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
5 *
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg" style="width: 542px; height: 222px;" />
8 * Input: head = [1,2,3,4,5], left = 2, right = 4
9 * Output: [1,4,3,2,5]
10 *
11 * Example 2:
12 *
13 * Input: head = [5], left = 1, right = 1
14 * Output: [5]
15 *
16 *
17 * Constraints:
18 *
19 * The number of nodes in the list is n.
20 * 1 <= n <= 500
21 * -500 <= Node.val <= 500
22 * 1 <= left <= right <= n
23 *
24 *
25 * Follow up: Could you do it in one pass?
26 */
27pub struct Solution {}
28use crate::util::linked_list::{ListNode, to_list};
29
30// problem: https://leetcode.com/problems/reverse-linked-list-ii/
31// discuss: https://leetcode.com/problems/reverse-linked-list-ii/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for singly-linked list.
36// #[derive(PartialEq, Eq, Clone, Debug)]
37// pub struct ListNode {
38// pub val: i32,
39// pub next: Option<Box<ListNode>>
40// }
41//
42// impl ListNode {
43// #[inline]
44// fn new(val: i32) -> Self {
45// ListNode {
46// next: None,
47// val
48// }
49// }
50// }
51impl Solution {
52 pub fn reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> {
53 Some(Box::new(ListNode::new(0)))
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_92() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.