增加 0024.两两交换链表中的节点的递归解法 go 版

This commit is contained in:
NevS
2021-05-27 00:13:54 +08:00
committed by GitHub
parent a24ee9c15a
commit b02a1c44f2

View File

@ -153,6 +153,19 @@ func swapPairs(head *ListNode) *ListNode {
}
```
```go
// 递归版本
func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
next := head.Next
head.Next = swapPairs(next.Next)
next.Next = head
return next
}
```
Javascript:
```javascript
var swapPairs = function (head) {