File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
S0206-reverse-linked-list Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments