Skip to content

Instantly share code, notes, and snippets.

@xudifsd
Created February 3, 2016 03:44
Show Gist options
  • Save xudifsd/73a4489e14e9b32359fd to your computer and use it in GitHub Desktop.
Save xudifsd/73a4489e14e9b32359fd to your computer and use it in GitHub Desktop.
struct ListNode<'a, T> where T: PartialOrd + 'a {
val: &'a T,
next: Option<Box<ListNode<'a, T>>>,
}
fn reverse_list<'a, T>(head: Option<Box<ListNode<'a, T>>>) -> Option<Box<ListNode<'a, T>>>
where T: 'a + PartialOrd {
if let Some(mut p) = head {
let mut tail = None;
loop {
let p_next = p.next.take();
p.next = tail;
tail = Some(p);
match p_next {
None => break,
_ => p = p_next.unwrap(),
};
};
tail
} else {
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment