Two Pointers: Unidirectional Traversal

TwoPointer-UnidrectionalTraversal.gif

def shift_zeros_to_the_end(nums: List[int])-> None:
 # The 'left' pointer is used to position non-zero elements.
 left = 0
 # Iterate through the array using a 'right' pointer to locate non-zero
 # elements.
 for right in range(len(nums)):
   if nums[right] != 0:
     nums[left], nums[right] = nums[right], nums[left]
     # Increment 'left' since it now points to a position already occupied
     # by a non-zero element.
     left += 1


Revision #3
Created 28 December 2024 22:12:35 by victor
Updated 9 February 2025 23:16:44 by victor