Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert
  • Loading branch information
songyzh committed Feb 2, 2020
commit a1971ec56bda0d6bad6fd1e6c9b0fb4c22359320
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod n0199_binary_tree_right_side_view;
mod n0282_expression_add_operators;
mod n0021_merge_two_sorted_lists;
mod n0129_sum_root_to_leaf_numbers;
mod n0206_reverse_linked_list;
mod n0131_palindrome_partitioning;
mod n0307_range_sum_query_mutable;
mod n0111_minimum_depth_of_binary_tree;
Expand Down
50 changes: 50 additions & 0 deletions src/n0206_reverse_linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* [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?
*
*/
pub struct Solution {}

use super::util::linked_list::{to_list, ListNode};

// submission codes start here

impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut curr = head;
let mut next = None;
while let Some(mut inner) = curr {
curr = inner.next.take();
inner.next = next;
next = Some(inner);
}
next
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_206() {
assert_eq!(
Solution::reverse_list(linked![1, 2, 3, 4, 5]),
linked![5, 4, 3, 2, 1]
);
}
}