Skip to content

Commit e89cfed

Browse files
authored
Merge pull request neetcode-gh#3094 from emanuele-em/patch-5
Update 0206-reverse-linked-list.rs
2 parents 4168e0f + e4a0dd0 commit e89cfed

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

rust/0206-reverse-linked-list.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
impl Solution {
22
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
3-
let mut node = None;
4-
while let Some(next) = head {
5-
node =
6-
Some (
7-
Box::new (
8-
ListNode {
9-
next : node,
10-
val : next.val,
11-
}
12-
)
13-
);
14-
head = next.next;
15-
}
16-
node
3+
let (mut prev, mut curr) = (None, head);
4+
while let Some(mut node) = curr{
5+
curr = node.next;
6+
node.next = prev;
7+
prev = Some(node);
8+
}
9+
pre
1710
}
1811
}

0 commit comments

Comments
 (0)