Update 0024.两两交换链表中的节点.md

This commit is contained in:
fw_qaq
2022-10-01 15:23:53 +08:00
committed by GitHub
parent 987380ca93
commit 00ad23f458

View File

@ -405,5 +405,26 @@ impl Solution {
}
```
```rust
// 递归
impl Solution {
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
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)
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>