mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update swap_every_two_nodes.py
This commit is contained in:
parent
93c18361f3
commit
69d068678b
@ -3,21 +3,20 @@
|
|||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
class ListNode:
|
class Node:
|
||||||
def __init__(self, val=0, next=None):
|
def __init__(self, val=0, next=None):
|
||||||
self.val = val
|
self.val = val
|
||||||
self.next = next
|
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:
|
if not head or not head.next:
|
||||||
return head
|
return head
|
||||||
|
|
||||||
## nodes to be swapped
|
|
||||||
first_node = head
|
first_node = head
|
||||||
second_node = head.next
|
second_node = head.next
|
||||||
|
|
||||||
# swapping
|
|
||||||
first_node.next = swap_pairs(second_node.next)
|
first_node.next = swap_pairs(second_node.next)
|
||||||
second_node.next = first_node
|
second_node.next = first_node
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user