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) {
- LibraryStack<Character> stack = new LibraryStack<>();
- if (list == null || list.size() < 2) {
- return true;
- }
- int n = list.size();
- for(int i = 0; i < n/2; i++) {
- stack.push(list.removeFirst());
- }
- if(n % 2 == 1) {
- list.removeFirst();
- }
- for(int i = 0; i < n/2; i++) {
- if(stack.pop() != list.removeFirst()) {
- return false;
- }
- }
- return true;
- }
- }
- // 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) {
- // LibraryStack<Character> stack = new LibraryStack<>();
- // if (list == null || list.size() <= 1) {
- // return true;
- // }
- // while(stack.size() != list.size()) {
- // char temp = list.removeFirst();
- // stack.push(temp);
- // if(list.size() == stack.size()+1) {
- // list.removeFirst();
- // }
- // }
- // while(!stack.isEmpty()) {
- // char temp = stack.pop();
- // char temp1 = list.removeFirst();
- // if (temp != temp1) {
- // return false;
- // }
- // }
- // return true;
- // }
- // }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement