Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class Solution {
  2. /**
  3. * Checks for a String represented as a SLList whether this String is a palindrome.
  4. * This is done by using a stack.
  5. *
  6. * An empty String or null should return true.
  7. *
  8. * @param list
  9. * SLList used to represent a String
  10. * @return true if the String represented as a SLList is a palindrome, otherwise false
  11. */
  12. public static boolean checkPalindrome(SLList list) {
  13. if (list == null || list.size() == 0){
  14. return true;
  15. }
  16. LibraryStack<Character> stack = new LibraryStack<>();
  17. int n = 0;
  18. if (list.size() % 2 == 0){
  19. n = list.size()/2;
  20. }
  21. if (list.size() % 1 == 0){
  22. n = (list.size() + 1)/2 ;
  23. }
  24. int counter = 0;
  25. while (counter != n){
  26. stack.push(list.removeFirst());
  27. counter++;
  28. }
  29.  
  30. while(!stack.isEmpty()){
  31. if (stack.pop().equals(list.removeFirst())){
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement