0
public boolean isPalindrome()
{

    Stack myStack = new Stack();
    for(Node current = head; current!=null; current = current.next)
    {
        if(!myStack.isEmpty())
        {
            if(myStack.peek()==current.data)
            {
                myStack.pop();
            }else if(current.next!=null&&myStack.peek()==current.next.data)
            {
                continue;
            }
            else
            {
                myStack.push(current.data);
            }
        }else
        {

            myStack.push(current.data);
        }   

    }

    return myStack.isEmpty();
}

What I am doing here is using a stack to check whether a linked list is a palindrome. It works as expected only thing is I wanted to get rid of code duplication where the else condition has a push of the data onto the stack.

Phoenix
  • 8,109
  • 15
  • 48
  • 83

5 Answers5

7

The algorithm is unfortunately not correct. For "abbaaa" it would report that that is a palindrome, although it isn't. Checking for palindromes without using the length is difficult.

abbaaa () -> push a
bbaaa (a) -> push b
baaa (ba) -> pop b
aaa (a) -> pop a
aa () -> push a
a (a) -> pop a
() -> palindrome
Daniel Fischer
  • 174,737
  • 16
  • 293
  • 422
2

This is a somewhat classic problem. There are many ways to solve it in java. One of the easiest is this one:

boolean isPalindrome(String s) {
   for (int i=0, len=s.length(); i<len/2; i++) {
      if (s.charAt(i) != s.charAt(len-i-1)) return false;
   }
   return true;
}

(Strictly speaking, this is a rewrite rather than a refactoring; however, any rewrite that preserves method signatures can be seen as a refactoring... and it is certainly more efficient)

tucuxi
  • 15,614
  • 2
  • 36
  • 70
1

If all you want to do is remove the code duplication between the two else conditions then remove them entirely.

public boolean isPalindrome()
{

    Stack myStack = new Stack();
    for(Node current = head; current!=null; current = current.next)
    {
        if(!myStack.isEmpty())
        {
            if(myStack.peek()==current.data)
            {
                myStack.pop();
                continue;
            }else if(current.next!=null&&myStack.peek()==current.next.data)
            {
                continue;
            }
        }                   
        myStack.push(current.data);             
    }

    return myStack.isEmpty();
}
Justin
  • 461
  • 4
  • 14
0

A simplification of functionality;

boolean isPalinDrome(String testString) {
    return new StringBuffer(testString).reverse().toString().equals(testString);
}
Reimeus
  • 152,723
  • 12
  • 195
  • 261
0

This should provide same functionality without repeat. It is pointed out however that your algorithm doesn't seem to be correct.

public boolean isPalindrome()
{

    Stack myStack = new Stack();
    boolean doPush;
    for(Node current = head; current!=null; current = current.next)
    {
        doPush = true;
        if(!myStack.isEmpty())
        {
            if(myStack.peek()==current.data)
            {
                doPush = false;
                myStack.pop();
            }else if(current.next!=null&&myStack.peek()==current.next.data)
            {
                doPush = false;
                continue;
            }
        }   
        if(doPush){                
            myStack.push(current.data);  
        }           
    }

    return myStack.isEmpty();
}
While-E
  • 1,447
  • 2
  • 19
  • 37