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 queue.
- *
- * 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() < 1) {
- return true;
- }
- LibraryQueue<Entry> queue = new LibraryQueue<>();
- if (list.size() % 2 == 0) {
- int half = list.size() / 2;
- for (int i = 0; i < half; i++) {
- char c = list.removeFirst();
- Entry e = new Entry(i, c);
- queue.enqueue(e);
- }
- for (int i = 0; i < half; i++) {
- if (queue.dequeue().getElement() != list.removeFirst()) {
- return false;
- }
- }
- }
- else {
- int half = (list.size() - 1) / 2;
- for (int i = 0; i < half; i++) {
- char c = list.removeFirst();
- Entry e = new Entry(i, c);
- queue.enqueue(e);
- }
- list.removeFirst();
- for (int i = 0; i < half; i++) {
- if (queue.dequeue().getElement() != list.removeFirst()) {
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement