143. Reorder List

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

Rust Solution

struct Solution;
use rustgym_util::*;
use std::collections::VecDeque;

impl Solution {
    fn reorder_list(head: &mut ListLink) {
        let mut p: ListLink = head.take();
        let mut deque: VecDeque<ListLink> = VecDeque::new();
        while let Some(mut n) = p {
            p = n.next.take();
            deque.push_back(Some(n));
        }
        let mut stack: Vec<ListLink> = vec![];
        let mut direction = true;
        while !deque.is_empty() {
            if direction {
                stack.push(deque.pop_front().unwrap());
            } else {
                stack.push(deque.pop_back().unwrap())
            }
            direction = !direction;
        }
        let mut prev: ListLink = None;
        while let Some(last) = stack.pop() {
            let mut node = last.unwrap();
            node.next = prev;
            prev = Some(node);
        }
        *head = prev
    }
}

#[test]
fn test() {
    let mut head = list!(1, 2, 3, 4, 5);
    Solution::reorder_list(&mut head);
    let res = list!(1, 5, 2, 4, 3);
    assert_eq!(head, res);
}

Having problems with this solution? Click here to submit an issue on github.