Skip to content

Commit 937e612

Browse files
committed
easy: 206 reverse linkedlist
1 parent 15f88df commit 937e612

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

solutions/reverseLinkedList.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
let mut prev = None;
20+
let mut current = head;
21+
22+
while let Some(mut boxed_node) = current {
23+
let next = boxed_node.next.take();
24+
boxed_node.next = prev;
25+
prev = Some(boxed_node);
26+
current = next;
27+
}
28+
29+
prev
30+
}
31+
}

0 commit comments

Comments
 (0)