61. Rotate List Medium
1/**
2 * [61] Rotate List
3 *
4 * Given the head of a linked list, rotate the list to the right by k places.
5 *
6 * Example 1:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg" style="width: 450px; height: 191px;" />
8 * Input: head = [1,2,3,4,5], k = 2
9 * Output: [4,5,1,2,3]
10 *
11 * Example 2:
12 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg" style="width: 305px; height: 350px;" />
13 * Input: head = [0,1,2], k = 4
14 * Output: [2,0,1]
15 *
16 *
17 * Constraints:
18 *
19 * The number of nodes in the list is in the range [0, 500].
20 * -100 <= Node.val <= 100
21 * 0 <= k <= 2 * 10^9
22 *
23 */
24pub struct Solution {}
25use crate::util::linked_list::{ListNode, to_list};
26
27// problem: https://leetcode.com/problems/rotate-list/
28// discuss: https://leetcode.com/problems/rotate-list/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32// Definition for singly-linked list.
33// #[derive(PartialEq, Eq, Clone, Debug)]
34// pub struct ListNode {
35// pub val: i32,
36// pub next: Option<Box<ListNode>>
37// }
38//
39// impl ListNode {
40// #[inline]
41// fn new(val: i32) -> Self {
42// ListNode {
43// next: None,
44// val
45// }
46// }
47// }
48impl Solution {
49 pub fn rotate_right(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
50 Some(Box::new(ListNode::new(0)))
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_61() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.