Swap Nodes in Pairs (Leetcode 24)
Problem Link: https://leetcode.com/problems/swap-nodes-in-pairs/
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if(head is None):
return None
if(head.next is None):
return head
temphead=self.swapPairs(head.next.next)
newhead=head.next
head.next.next=head
head.next=temphead
return newhead
Last updated