Update 0024.两两交换链表中的节点.md
This commit is contained in:
parent
5043002ed6
commit
425f407d6c
|
|
@ -129,6 +129,23 @@ class Solution {
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def swapPairs(self, head: ListNode) -> ListNode:
|
||||||
|
dummy = ListNode(0) #设置一个虚拟头结点
|
||||||
|
dummy.next = head
|
||||||
|
cur = dummy
|
||||||
|
while cur.next and cur.next.next:
|
||||||
|
tmp = cur.next #记录临时节点
|
||||||
|
tmp1 = cur.next.next.next #记录临时节点
|
||||||
|
|
||||||
|
cur.next = cur.next.next #步骤一
|
||||||
|
cur.next.next = tmp #步骤二
|
||||||
|
cur.next.next.next = tmp1 #步骤三
|
||||||
|
|
||||||
|
cur = cur.next.next #cur移动两位,准备下一轮交换
|
||||||
|
return dummy.next
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue