206. Reverse Linked List

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Rust Solution

struct Solution;
use rustgym_util::*;

impl Solution {
    fn reverse_list(head: ListLink) -> ListLink {
        let mut p = head;
        let mut prev = None;
        while let Some(mut node) = p {
            p = node.next;
            node.next = prev;
            prev = Some(node);
        }
        prev
    }
}

#[test]
fn test() {
    let head = list!();
    let res = list!();
    assert_eq!(Solution::reverse_list(head), res);
    let head = list!(1);
    let res = list!(1);
    assert_eq!(Solution::reverse_list(head), res);
    let head = list!(1, 2);
    let res = list!(2, 1);
    assert_eq!(Solution::reverse_list(head), res);
    let head = list!(1, 2, 3);
    let res = list!(3, 2, 1);
    assert_eq!(Solution::reverse_list(head), res);
}

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