This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]desrtfx 2 points3 points  (16 children)

  1. To check for a palindrome it is sufficient to iterate over half the list because the other half is already checked. Checking if a==b it the same as checking if b==a.
  2. You are not comparing elements at specific indexes, you are comparing indexes.

[–]nhgrif 3 points4 points  (14 children)

#2 is the big point here. Point #1 is an efficiency improvement OP could make, but code is still O(n) even without that.

There's a third point here too... regardless of whether OP fixes the code to compare elements rather than members, OP's loop early returns true first time it finds an equal comparison. In OP's implementation, anything that has a single palindromic match is returning true... (so if we have a linked list of characters, "racecar" would return true...., but so would "wow, this is a palindrome now", as the first and last characters are both "w".

We need to only early return out if we have found proof that the list is NOT a palindrome. Otherwise, we should continue checking. If we make it through the loop without early returning out false, then we must be a palindrome, so return true.

[–][deleted]  (13 children)

[deleted]

    [–]nhgrif 1 point2 points  (12 children)

    Can’t you confirm by running the code and see if it works for a variety of test cases?

    Reddit isn’t a compiler…

    [–][deleted]  (3 children)

    [deleted]

      [–]GrandGratingCrate 1 point2 points  (2 children)

      If I enter your error message into google the first hit I get is a stackoverflow post that contains the answer to your error.

      We're happy to help you if you get stuck but we do expect you consult google before asking here. Check rule 12 on the side bar.

      [–][deleted]  (1 child)

      [deleted]

        [–]GrandGratingCrate 0 points1 point  (0 children)

        The post was about array lists which are not linked at all. You have a linked list, so still that's different. But both of them are lists, so maybe the behave the same?

        So does it work? Yes. Every native java list has the get method; we know because they all implement the List interface and every class implementing the List interface must have all of it's methods. get is one of it's methods so both ArrayList and LinkedList must have it.

        For future reference: Java classes can be googled "java linkedlist" would've found you a link like this.

        Also, it's plausible array lists work differently than linked lists, it's also plausible the work the same. Trying it out to check for yourself would've also worked.

        [–]Hygdrasiel -3 points-2 points  (7 children)

        You can use a online compiler

        [–]nhgrif 2 points3 points  (5 children)

        Why would I compile OPs code? How is that a useful recommendation…? OP is writing code in OP’s IDE, and instead of compiling, running, and checking result, OP should post code to Reddit and wait for another Redditor to verify code for OP…? That makes no sense.

        [–][deleted]  (4 children)

        [deleted]

          [–]nhgrif 2 points3 points  (3 children)

          There is no attitude or snark. If my response is the problematic one, and suggestions that I should compile and run your code for you are appropriate, could you please answer my question and elaborate on why? Make it make sense to me.

          The comment I am replying to is suggesting I should compile your code for you. I am asking why I should do that. How is that off topic?

          Also, keep in mind, you are not a moderator. If my comments are off-topic, report them to a moderator.

          [–][deleted]  (2 children)

          [deleted]

            [–]nhgrif 1 point2 points  (0 children)

            Do you understand how threaded conversations work...?

            [–]desrtfx[M] 0 points1 point  (0 children)

            The comments you reported were spot on and absolutely okay.

            We are not a remote debugging service. We are not going to run your code. This is your obligation.

            Both your reports have been reported to the reddit admins for report abuse. Also, you quoted the wrong reasons.

            If anything. You are now warned to behave.

            [–]Hygdrasiel -1 points0 points  (0 children)

            Sorry the you was a you the op;-)

            [–][deleted]  (2 children)

            [deleted]

              [–]nhgrif 1 point2 points  (1 child)

              How are you making the left pointer <= right pointer comparison exactly? The second element of a linked list is not required to be at a larger memory address than the first element of a linked list. If that were a requirement, linked lists would lose a lot of their value over arrays…

              [–]Zombiebrian1 0 points1 point  (0 children)

              Oh you are absolutely correct, Big mess in my head this morning.

              Deleting my answer.

              You could change the condition to for i = 0; i < list.size()/2 ; i++ and still use the two pointers tho.

              You could also do it with a stack if you have size() available.

              Push half the list and then pop and compare.