O(1) space complexity and O(n) time complexity