MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / swapPairs

Method swapPairs

leetcode/24. Swap Nodes in Pairs.py:16–32  ·  view source on GitHub ↗
(self, head)

Source from the content-addressed store, hash-verified

14
15class Solution(object):
16 def swapPairs(self, head):
17 if head == None or head.next == None:
18 return head
19 cur = head
20 head = head.next
21 while cur and cur.next:
22 pNext = cur.next.next
23 cur.next.next = cur
24 if pNext:
25 if pNext.next:
26 cur.next = pNext.next
27 else:
28 cur.next = pNext
29 else:
30 cur.next = None
31 cur = pNext
32 return head
33 # recursion
34 def swapPairs2(self, head):
35 if not head or not head.next:

Callers 1

swapPairs2Method · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected