mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0024.两两交换链表中的节点.md
This commit is contained in:
@ -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>
|
||||
|
Reference in New Issue
Block a user