- Published on
Single linked list palindrome
- Authors
- Name
- Moch Lutfi
- @kaptenupi
Problem
Determine whether or not the single linked list is a palindrome.
Solution
In the single linked list case determine palindrome is tricky, because the node only forward and the size is uknown without iterate through the linked list. That means you can't know whether a linked list is palindrome or not without traversing through the linked list.
- Find the center point of the node list, using double steps (fast var) and reverse the first half of the list.
- Walk on the second half and walk back in the first to the first difference.
- Return false if found diff, or true if all items are equal.