Skip to content

Commit a1971ec

Browse files
committed
revert
1 parent b8b0d94 commit a1971ec

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ mod n0199_binary_tree_right_side_view;
231231
mod n0282_expression_add_operators;
232232
mod n0021_merge_two_sorted_lists;
233233
mod n0129_sum_root_to_leaf_numbers;
234+
mod n0206_reverse_linked_list;
234235
mod n0131_palindrome_partitioning;
235236
mod n0307_range_sum_query_mutable;
236237
mod n0111_minimum_depth_of_binary_tree;

src/n0206_reverse_linked_list.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [206] Reverse Linked List
3+
*
4+
* Reverse a singly linked list.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: 1->2->3->4->5->NULL
10+
* Output: 5->4->3->2->1->NULL
11+
*
12+
*
13+
* Follow up:
14+
*
15+
* A linked list can be reversed either iteratively or recursively. Could you implement both?
16+
*
17+
*/
18+
pub struct Solution {}
19+
20+
use super::util::linked_list::{to_list, ListNode};
21+
22+
// submission codes start here
23+
24+
impl Solution {
25+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
26+
let mut curr = head;
27+
let mut next = None;
28+
while let Some(mut inner) = curr {
29+
curr = inner.next.take();
30+
inner.next = next;
31+
next = Some(inner);
32+
}
33+
next
34+
}
35+
}
36+
37+
// submission codes end
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
43+
#[test]
44+
fn test_206() {
45+
assert_eq!(
46+
Solution::reverse_list(linked![1, 2, 3, 4, 5]),
47+
linked![5, 4, 3, 2, 1]
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)