Skip to content

Commit

Permalink
Update 0206-reverse-linked-list.rs
Browse files Browse the repository at this point in the history
cleaner solution than the original
  • Loading branch information
emanuele-em authored Oct 23, 2023
1 parent a88f493 commit e4a0dd0
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions rust/0206-reverse-linked-list.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
impl Solution {
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut node = None;
while let Some(next) = head {
node =
Some (
Box::new (
ListNode {
next : node,
val : next.val,
}
)
);
head = next.next;
}
node
let (mut prev, mut curr) = (None, head);
while let Some(mut node) = curr{
curr = node.next;
node.next = prev;
prev = Some(node);
}
pre
}
}

0 comments on commit e4a0dd0

Please sign in to comment.