blob: 289d8dd185431c638cb160c200ae0abcdaaf9bd5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
pub struct Solution;
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
fn new(val: i32) -> self {
ListNode {
next: None,
val
}
}
}
impl Solution {
pub fn delete_middle(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut slowPtr = &head;
let mut fastPtr = &head;
let mut fakeHead = Box::new(ListNode::new(-1));
let mut fakeTmp = &mut *fakeHead as *mut ListNode;
let mut slowPrevPtr = &mut *fakeHead as *mut ListNode;
while fastPtr.is_some() && (fastPtr).as_ref().unwrap().next.is_some() {
unsafe {
let mut next = Box::new(ListNode::new((slowPtr).as_ref().unwrap().val));
let nextPtr = &mut *next as *mut ListNode;
(*fakeTmp).next = Some(next);
slowPrevPtr = fakeTmp;
fakeTmp = nextPtr;
slowPtr = &(slowPtr).as_ref().unwrap().next;
fastPtr = &(fastPtr).as_ref().unwrap().next;
if fastPtr.is_some() {
fastPtr = &(fastPtr).as_ref().unwrap().next;
}
}
}
slowPtr = &(slowPtr).as_ref().unwrap().next;
while slowPtr.is_some() {
unsafe {
let mut next = Box::new(ListNode::new((slowPtr).as_ref().unwrap().val));
let nextPtr = &mut *next as *mut ListNode;
(*fakeTmp).next = Some(next);
fakeTmp = nextPtr;
slowPtr = &(slowPtr).as_ref().unwrap().next;
}
}
fakeHead.next
}
}
fn main() {}
|