Skip to content

Commit 2cb901b

Browse files
author
caiminchao
committed
leetcode 24
1 parent 95a32c9 commit 2cb901b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

solution.py

+21
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,27 @@ def mergeTwoLists(self, l1, l2):
430430
l2.next = self.mergeTwoLists(l1, l2.next)
431431
return l2
432432

433+
# leetcode24
434+
def swapPairs(self, head):
435+
"""
436+
:type head: ListNode
437+
:rtype: ListNode
438+
"""
439+
temp = ListNode(0)
440+
temp.next = head
441+
l = temp
442+
443+
while l.next !=None and l.next.next !=None:
444+
first = l.next
445+
second = l.next.next
446+
447+
first.next = second.next
448+
449+
second.next = first
450+
l.next = second
451+
l = l.next.next
452+
453+
return temp.next
433454

434455

435456
if __name__ == '__main__':

0 commit comments

Comments
 (0)