Update swap_every_two_nodes.py

This commit is contained in:
marina 2023-08-07 15:54:37 -07:00 committed by GitHub
parent 93c18361f3
commit 69d068678b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,21 +3,20 @@
# author: bt3gl
class ListNode:
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def swap_pairs(head: Optional[ListNode]) -> Optional[ListNode]:
def swap_pairs(head: Optional[Node]) -> Optional[Node]:
if not head or not head.next:
return head
## nodes to be swapped
first_node = head
second_node = head.next
# swapping
first_node.next = swap_pairs(second_node.next)
second_node.next = first_node