A simple solution is to use a stack of list nodes. This mainly involves three steps.
Traverse the given list from head to tail and push every visited node to stack.
Traverse the list again. For every visited node, pop a node from the stack and compare data of popped node with the currently visited node.
If all nodes matched, then return true, else false.
Below image is a dry run of the above approach:
Below is the implementation of the above approach :
# Python3 program to check if linked
# list is palindrome using stack
# Function to check if the linked list
# is palindrome or not
# Temp pointer
# Declare a stack
# Push all elements of the list
# to the stack
# Move ahead
# Iterate in the list again and
# check by popping from the stack
# Get the top most element
# Check if data is not
# same as popped element
# Move ahead
# Driver Code
# Addition of linked list
# Initialize the next pointer
# of every current pointer
# Call function to check
# palindrome or not
# This code is contributed by Nishtha Goel
Time complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for using a stack, where n represents the length of the given linked list.
METHOD 2 (By reversing the list): This method takes O(n) time and O(1) extra space. 1) Get the middle of the linked list. 2) Reverse the second half of the linked list. 3) Check if the first half and second half are identical. 4) Construct the original linked list by reversing the second half again and attaching it back to the first half
To divide the list into two halves, method 2 of this post is used.
When a number of nodes are even, the first and second half contain exactly half nodes. The challenging thing in this method is to handle the case when the number of nodes is odd. We don’t want the middle node as part of the lists as we are going to compare them for equality. For odd cases, we use a separate variable ‘midnode’.
# Python program to check if
# linked list is palindrome
# Node class
# Constructor to initialize
# the node object
# Function to initialize head
# Function to check if given
# linked list is palindrome or not
# To handle odd size list
# Initialize result
# Get the middle of the list.
# Move slow_ptr by 1 and
# fast_ptrr by 2, slow_ptr
# will have the middle node
# We need previous of the slow_ptr
# for linked lists with odd
# fast_ptr would become NULL when
# there are even elements in the
# list and not NULL for odd elements.
# We need to skip the middle node for
# odd case and store it somewhere so
# that we can restore the original list
# Now reverse the second half
# and compare it with the first half
# NULL terminate first half
# Reverse the second half
res =self.compareLists(head, second_half)
# Construct the original list back
# Reverse the second half again
# If there was a mid node (odd size
# case) which was not part of either
# first half or second half.
# Function to reverse the linked list
# Note that this function may change
# the head
# Function to check if two input
# lists have same data
defcompareLists(self, head1, head2):
# Both are empty return 1
if(temp1 ==Noneandtemp2 ==None):
# Will reach here when one is NULL
# and other is not
# Function to insert a new node
# at the beginning
# Allocate the Node &
# Put in the data
# Link the old list of the new one
# Move the head to point to the
# new Node
# A utility function to print
# a given linked list
print(temp.data, end ="->")
# Driver code
s =['a', 'b', 'a',
'c', 'a', 'b', 'a']
# This code is contributed by MuskanKalra1
Please Login to comment...