-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathanswer.py
More file actions
26 lines (22 loc) · 691 Bytes
/
answer.py
File metadata and controls
26 lines (22 loc) · 691 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
#!/usr/bin/python
#------------------------------------------------------------------------------
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
prev, prev.next = self, head
while prev.next and prev.next.next:
a = prev.next
b = a.next
prev.next, b.next, a.next = b, a, b.next
prev = a
return self.next
#------------------------------------------------------------------------------
#Testing