This is the solution i have given.
class Solution {
public boolean isPalindrome(ListNode head) {
String s= "";
String p= "";
while(head != null){
s= s+head.val;
p = head.val+p;
head = head.next;
}
return s.equals(p);
}
}
I can understand i am creating multiple strings in string pool. but it doesnt show memory exceeded. it shows "Testcases passed, but took too long."
Can someone kindly explain how is that exceeding time limit whereas all the other solutions has minmum of two loops? be it two pointer solution or the list solution or the stack solution.
I was expecting it to run properly, because I am just using one loop to create both the reverse and normal string and as far as i know equals is a O(1) process. So how does the time limit exceeds?


1→11which is not a palindrome, but your function will determine it is.