提交 f487b61f 编写于 作者: jhaos's avatar jhaos

Add new file

上级 ff24b2ad
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
**你不能只是单纯的改变节点内部的值**,而是需要实际的进行节点交换。
**示例:**
```
给定 1->2->3->4, 你应该返回 2->1->4->3.
```
通过次数`161,345` | 提交次数`240,479`
**代码实现**
```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
cur = ListNode(0)
cur.next = head
k = cur
while head and head.next:
p,q = head,head.next
k.next = q
q.next,p.next = p, p.next.next
head = head.next
k = k.next.next
return cur.next
```
```
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册