Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- /**
- * Checks for a String represented as a SLList whether this String is a palindrome.
- * This is done by using a stack.
- *
- * An empty String or null should return true.
- *
- * @param list
- * SLList used to represent a String
- * @return true if the String represented as a SLList is a palindrome, otherwise false
- */
- public static boolean checkPalindrome(SLList list) {
- if (list == null || list.size() == 0){
- return true;
- }
- LibraryStack<Character> stack = new LibraryStack<>();
- int n = 0;
- if (list.size() % 2 == 0){
- n = list.size()/2;
- }
- if (list.size() % 1 == 0){
- n = (list.size() + 1)/2 ;
- }
- int counter = 0;
- while (counter != n){
- stack.push(list.removeFirst());
- counter++;
- }
- while(!stack.isEmpty()){
- if (stack.pop().equals(list.removeFirst())){
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement