258. Add Digits LeetCode Solution
In this guide, you will get 258. Add Digits LeetCode Solution with the best time and space complexity. The solution to Add Digits problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Add Digits solution in C++
- Add Digits solution in Java
- Add Digits solution in Python
- Additional Resources

Problem Statement of Add Digits
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
Example 1:
Input: num = 38
Output: 2
Explanation: The process is
38 –> 3 + 8 –> 11
11 –> 1 + 1 –> 2
Since 2 has only one digit, return it.
Example 2:
Input: num = 0
Output: 0
Constraints:
0 <= num <= 231 – 1
Follow up: Could you do it without any loop/recursion in O(1) runtime?
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
258. Add Digits LeetCode Solution in C++
class Solution {
public:
int addDigits(int num) {
return 1 + (num - 1) % 9;
}
};
/* code provided by PROGIEZ */
258. Add Digits LeetCode Solution in Java
class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
// code provided by PROGIEZ
258. Add Digits LeetCode Solution in Python
class Solution:
def addDigits(self, num: int) -> int:
return 0 if num == 0 else 1 + (num - 1) % 9
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.