diff --git a/24-swap-nodes-in-pairs/24-swap-nodes-in-pairs.py b/24-swap-nodes-in-pairs/24-swap-nodes-in-pairs.py new file mode 100644 index 0000000..6075a5e --- /dev/null +++ b/24-swap-nodes-in-pairs/24-swap-nodes-in-pairs.py @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head == None or head.next == None: + return head + + first = head + second = head.next + + first.next = second.next + second.next = first + + first.next = self.swapPairs(first.next) + + return second \ No newline at end of file