Skip to content

Commit

Permalink
Solve leetcode 206
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseibel1 committed Oct 2, 2023
1 parent 56f6ea6 commit 692bffc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lc206.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use log::warn;

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}

impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode {
next: None,
val,
}
}
}

pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut curr: Option<Box<ListNode>> = None;
let mut next: Option<Box<ListNode>> = head;
loop {
if next.is_none() {
break
}
let mut next_node: Box<ListNode> = next.take().unwrap();
let fwd = next_node.next.take();
next_node.next = curr.take();
curr = Some(next_node);
next = fwd;
}
curr
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod lc217;
pub mod lc242;
pub mod lc74;
pub mod lc143;
pub mod lc206;

0 comments on commit 692bffc

Please sign in to comment.