# 两两交换链表中的节点

<p>给定一个链表，两两交换其中相邻的节点，并返回交换后的链表。</p><p><strong>你不能只是单纯的改变节点内部的值</strong>，而是需要实际的进行节点交换。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/88da15e3bf83dfb0c4784dc6700e84bc.jpeg" style="width: 422px; height: 222px;" /><pre><strong>输入：</strong>head = [1,2,3,4]<strong><br />输出：</strong>[2,1,4,3]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>head = [1]<strong><br />输出：</strong>[1]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点的数目在范围 <code>[0, 100]</code> 内</li>	<li><code>0 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶：</strong>你能在不修改链表节点值的情况下解决这个问题吗?（也就是说，仅修改节点本身。）</p>

## template

```python
class ListNode(object):
	def __init__(self, x):
		self.val = x
		self.next = None
class LinkList:
	def __init__(self):
		self.head=None
	def initList(self, data):
		self.head = ListNode(data[0])
		r=self.head
		p = self.head
		for i in data[1:]:
			node = ListNode(i)
			p.next = node
			p = p.next
		return r
	def	convert_list(self,head):
		ret = []
		if head == None:
			return
		node = head
		while node != None:
			ret.append(node.val)
			node = node.next
		return ret
class Solution(object):
	def swapPairs(self, head):
		dummyHead = ListNode(-1)
		dummyHead.next = head
		prev, p = dummyHead, head
		while p != None and p.next != None:
			q, r = p.next, p.next.next
			prev.next = q
			q.next = p
			p.next = r
			prev = p
			p = r
		return dummyHead.next
# %%
l = LinkList()
head = [1,2,3,4]
l1 = l.initList(head)
s = Solution()
print(l.convert_list(s.swapPairs(l1)))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```