Skip to content

Commit 40bd0dd

Browse files
committed
add 206
1 parent 3833082 commit 40bd0dd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

S0206-reverse-linked-list/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "S0206-reverse-linked-list"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
leetcode_prelude="0.1"

S0206-reverse-linked-list/src/main.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use leetcode_prelude::*;
2+
fn main() {
3+
let l = linkedlist![1,2,3,4];
4+
println!("{:?}", Solution::reverse_list(l));
5+
}
6+
7+
impl Solution {
8+
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
9+
let cur = &mut head;
10+
let mut prev = None;
11+
while cur.is_some() {
12+
let mut cur_next = std::mem::replace(&mut cur.as_mut().unwrap().next, prev.take());
13+
prev = cur.take();
14+
std::mem::swap(&mut cur_next, cur);
15+
}
16+
17+
prev
18+
}
19+
}
20+
struct Solution;
21+
22+
/*
23+
impl Solution {
24+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
25+
let mut head = head; //move
26+
let mut new_head:Option<Box<ListNode>> = None;
27+
loop {
28+
if head.is_none() {
29+
break;
30+
}
31+
let val = head.as_mut().unwrap().val;
32+
let mut new_node = ListNode::new(val);
33+
if new_head.is_some() {
34+
new_node.next = new_head;
35+
}
36+
new_head = Some(Box::new(new_node));
37+
head = head.unwrap().next;
38+
}
39+
new_head
40+
}
41+
}
42+
*/

0 commit comments

Comments
 (0)