[][src]Function leetcode_for_rust::cd0025_reverse_nodes_in_k_group::reverse_k_group

pub fn reverse_k_group(
    head: Option<Box<ListNode>>,
    k: i32
) -> Option<Box<ListNode>>

Solutions

Approach 1: Recursion

  • Time complexity: O(2n)

  • Space complexity: O(n)

  • Runtime: 0ms

Memory: 2.7MB


// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
        let mut head = head;
        let mut tail = &mut head;

        for _ in 0..k {
            match tail.as_mut() {
                None => return head,
                Some(tail_ref) => tail = &mut tail_ref.next,
            }
        }

        let tail = tail.take();
        Solution::add(head, Solution::reverse_k_group(tail, k))
    }

    pub fn add(head: Option<Box<ListNode>>, tail: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
        let mut head = head;
        let mut tail = tail;

        while let Some(mut new_tail) = head.take() {
            head = new_tail.next.take();
            new_tail.next = tail.take();
            tail = Some(new_tail);
        }
        tail
    }
}