Skip to content

Commit 5c94c03

Browse files
committed
Update solution 024 [Python3]
1 parent bffcf3a commit 5c94c03

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def swapPairs(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
if (not head) or (not head.next):
14+
return head
15+
pre=head.next
16+
p=head
17+
q=head.next
18+
19+
while q:
20+
t=q.next
21+
q.next=p
22+
if (not t) or (not t.next):
23+
p.next=t
24+
break
25+
p.next=t.next
26+
p=t
27+
q=p.next
28+
return pre

0 commit comments

Comments
 (0)