234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Follow up: Could you do it in O(n) time and O(1) space?
Idea: Use two iterators, one fast, and one slow. Have slow iterate ++, and fast x2. Add data contained by the slow iterator into a stack. Keep track of the size along the way, as it will be used to determine whether we need to increment the slow iterator another time. Popping from the top of the stack effectively reverses the linked list, so if we continue iterating with the slow iterator we can confirm matching characters.
Last updated