Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. // Definition for singly-linked list.
  2. #[derive(PartialEq, Eq, Clone, Debug)]
  3. pub struct ListNode {
  4. pub val: i32,
  5. pub next: Option<Box<ListNode>>
  6. }
  7.  
  8. impl ListNode {
  9. #[inline]
  10. fn new(val: i32) -> Self {
  11. ListNode {
  12. next: None,
  13. val
  14. }
  15. }
  16. }
  17.  
  18. struct Solution {}
  19.  
  20. impl Solution {
  21. pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
  22. let mut prev = None;
  23. let mut cur = head;
  24. let mut next = None;
  25. while cur.is_some() {
  26. let mut n = cur.take().unwrap();
  27. next = n.next.take();
  28. n.next = prev;
  29. cur = next;
  30. prev = Some(n);
  31. }
  32. prev
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement