✅ Beats 98.7% solutions,✅ Easy to understand,✅ single for loop O(n) TC in Python by ✅ BOLT CODING ✅
Explanation\nWe are iterating from back to the list as we need to add at the end. We are checking if the current digit is 9. That means we have to replace 9 wit
# Explanation\nWe are iterating from back to the list as we need to add at the end. We are checking if the current digit is 9. That means we have to replace 9 with 0 and have a carry of 1. If thats not the case then we don\'t have to iterate through whole loop, we can simply return the list of digit from there.\n\nIn case its running until the loop ends, that means all the digits are 9. So inorder to insert a 1 at 0 position we appened with an additional 0 at the end and changed digit[0] to 1. Now there are various ways to do the same, we can use insert method as well.\n# Complexity\n- Time complexity: O(n) as we are using a single loop and worst case if all the digits are 9 we have to iterate through whole loop \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) as we haven\'t used any additional variable to store the number.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def plusOne(self, digit: List[int]) -> List[int]:\n        for i in range(len(digit)-1, -1, -1):\n            if digit[i] == 9:\n                digit[i] = 0\n            else:\n                digit[i]+=1\n                return digit\n    \n        digit.append(0)            \n        digit[0] = 1\n        return digit\n        \n\n        \n```\n# Learning\nTo understand problems in simpler ways, need help with projects, want to learn coding from scratch, work on resume level projects, learn data science ...................\n\nSubscribe to Bolt Coding Channel - https://www.youtube.com/@boltcoding
['Python3']