Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- }
- }
- }
- struct Solution {}
- impl Solution {
- pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
- let mut prev = None;
- let mut cur = head;
- let mut next = None;
- while cur.is_some() {
- let mut n = cur.take().unwrap();
- next = n.next.take();
- n.next = prev;
- cur = next;
- prev = Some(n);
- }
- prev
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement