Given the head
of a singly linked list, return true
if it is a
or false
otherwise.
Example 1:
Input: head = [1,2,2,1] Output: true
Example 2:
Input: head = [1,2] Output: false
Constraints:
[1, 105]
.0 <= Node.val <= 9
class Solution {
public boolean isPalindrome(ListNode head) {
Stack st = new Stack<>();
ListNode ptr = head ;
while(ptr!=null){
st.push(ptr.val);
ptr = ptr.next;
}
ListNode ptr2 = head ;
while(ptr2!=null){
int val1 = ptr2.val;
int val2 = st.pop();
if(val1 != val2 ){
return false ;
}
ptr2 = ptr2.next;
}
return true ;
}
}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India