diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 3b86ac6f..d65d0bca 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -405,5 +405,26 @@ impl Solution { } ``` +```rust +// 递归 +impl Solution { + pub fn swap_pairs(head: Option>) -> Option> { + if head == None || head.as_ref().unwrap().next == None { + return head; + } + + let mut node = head.unwrap(); + + if let Some(mut next) = node.next.take() { + node.next = Solution::swap_pairs(next.next); + next.next = Some(node); + Some(next) + } else { + Some(node) + } + } +} +``` + -----------------------