LeetCode — Palindrome

1 min readApr 15, 2025

I like to add different ways to solve problems on Leetcode. Today, we will see the Palindrome checker.

Problem: https://leetcode.com/problems/palindrome-number/

Solution 1: 5ms | O(n)

/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0) return false;

let original = x;
let reversed = 0;
let lastDigit;

while (x > 0) {
lastDigit = x % 10;
reversed = reversed * 10 + lastDigit;
x = Math.floor(x / 10);
}

return original === reversed;
};

Solution 2: 3ms | O(n)

/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0) return false;

let original = x;
let reversed = 0;
let lastDigit;

while (x > 0) {
lastDigit = x % 10;
reversed = reversed * 10 + lastDigit;
x = Math.floor(x / 10);
}

return original === reversed;
};

Observation: The leetcode timings for time and space usage are not correct. It gives different readings for different submissions for the same solution.

Will give less priority to the estimates from now on probably.

--

--

Visakh Vijayan
Visakh Vijayan

Written by Visakh Vijayan

Techie from Kerala, India. Days are for coding, nights for weaving tales of tech, travel, and finance. Join me in exploring this multifaceted journey

No responses yet