-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.py
More file actions
34 lines (31 loc) · 638 Bytes
/
24.py
File metadata and controls
34 lines (31 loc) · 638 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
33
34
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p = head
i = 0
while p:
if i%2 == 0 and p.next !=None:
p.val, p.next.val = p.next.val, p.val
i+=1
p=p.next
return head
head = ListNode(1)
r = head
nums = [2,3,4]
for x in nums:
node = ListNode(x)
r.next = node
r = node
test = Solution()
p = test.swapPairs(head)
while p:
print p.val
p = p.next