forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0024.py
More file actions
32 lines (26 loc) · 714 Bytes
/
0024.py
File metadata and controls
32 lines (26 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
dummy = ListNode(None)
dummy.next = head
len = 0
curr = head
while curr:
len += 1
curr = curr.next
prev = dummy
curr = head
for i in range(len // 2):
next = curr.next
curr.next = next.next
next.next = prev.next
prev.next = next
prev = curr
curr = curr.next
return dummy.next