Skip to content

Commit 2581b7f

Browse files
committed
🐱(link): 24. 两两交换链表中的节点
补充 go 语言
1 parent c075793 commit 2581b7f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/data-structure/linked_list/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ class Solution:
222222

223223
优雅递归:
224224

225+
<!-- tabs:start -->
226+
227+
#### **Python**
228+
225229
```python
226230
# Definition for singly-linked list.
227231
# class ListNode:
@@ -243,6 +247,33 @@ class Solution:
243247
return second_node
244248
```
245249

250+
#### **Go**
251+
252+
```go
253+
/**
254+
* Definition for singly-linked list.
255+
* type ListNode struct {
256+
* Val int
257+
* Next *ListNode
258+
* }
259+
*/
260+
func swapPairs(head *ListNode) *ListNode {
261+
if head == nil || head.Next == nil {
262+
return head
263+
}
264+
265+
firstNode := head
266+
secondNode := head.Next
267+
268+
firstNode.Next = swapPairs(secondNode.Next)
269+
secondNode.Next = firstNode
270+
271+
return secondNode
272+
}
273+
```
274+
275+
<!-- tabs:end -->
276+
246277
## 25. K 个一组翻转链表
247278

248279
[原题链接](https://leetcode-cn.com/problems/reverse-nodes-in-k-group/)

0 commit comments

Comments
 (0)