LeetCode每日一题(2074. Reverse Nodes in Even Length Groups)

该问题涉及处理链表,将节点按自然数序列分组,并反转偶数长度的组。给定链表的头结点,代码实现首先拆解链表到数组,然后反转偶数长度的组,最后重组链表并返回头结点。

You are given the head of a linked list.

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,

The 1st node is assigned to the first group.
The 2nd and the 3rd nodes are assigned to the second group.
The 4th, 5th, and 6th nodes are assigned to the third group, and so on.
Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.

Reverse the nodes in each group with an even length, and return the head of the modified linked list.

Example 1:

Input: head = [5,2,6,3,9,1,7,3,8,4]
Output: [5,6,2,3,9,1,4,8,3,7]
Explanation:

  • The length of the first group is 1, which is odd, hence no reversal occurs.

  • The length of the second group is 2, which is even, hence the nodes are reversed.

  • The length of the third group is 3, which is odd, hence no reversal occurs.

  • The length of the last group is 4, which is even, hence the nodes are reversed.

    Example 2:

Input: head = [1,1,0,6]
Output: [1,0,1,6]
Explanation:

  • The length of the first group is 1. No reversal occurs.

  • The length of the second group is 2. The nodes are reversed.

  • The length of the last group is 1. No reversal occurs.

    Example 3:

Input: head = [1,1,0,6,5]
Output: [1,0,1,5,6]
Explanation:

  • The length of the first group is 1. No reversal occurs.
  • The length of the second group is 2. The nodes are reversed.
  • The length of the last group is 2. The nodes are reversed.

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 105

因为是根据每组的实际长度来决定是否反向, 所以如果想用函数式的方式来解这个题, 最后一组会比较难处理, 反正我是没想到好的办法。只能退而求其次用更直观的方法, 就是把整个 list 拆散, 将 nodes 放到一个数组里, 同时将偶数长度的组进行反向, 最终再把数组重新组成 list


impl Solution {
    pub fn reverse_even_length_groups(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
        let mut arr = Vec::new();
        let mut tmp = Vec::new();
        let mut group = 1;
        while let Some(mut node) = head {
            head = node.next.take();
            tmp.push(node);
            if tmp.len() == group {
                if group % 2 == 0 {
                    tmp.reverse();
                }
                arr.append(&mut tmp);
                tmp = Vec::new();
                group += 1;
                continue;
            }
        }
        if tmp.len() % 2 == 0 {
            tmp.reverse();
        }
        arr.append(&mut tmp);
        let mut tail = None;
        while let Some(mut node) = arr.pop() {
            node.next = tail;
            tail = Some(node);
        }
        tail
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值