Question: class Node: def _ _ init _ _ ( self , data ) : self.data = data self.next = None def length ( head )
class Node:
def initself data:
self.data data
self.next None
def lengthhead:
count
while head:
count
head head.next
return count
def checkpalindromehead:
l lengthhead
t head
mid l
ispalindrome True
# Traverse to the midpoint
for i in rangemid:
t tnext
# Reverse the second half of the list
prev None
while t:
nextnode tnext
tnext prev
prev t
t nextnode
# Compare the first half and the reversed second half
t head
t prev
for i in rangemid:
if tdata tdata:
ispalindrome False
break
t tnext
t tnext
return ispalindrome
def llarr:
if lenarr:
return None
head Nodearr
last head
for data in arr::
last.next Nodedata
last last.next
return head
# Main
# Read the linked list elements including
arr listinti for i in inputstripsplit
# Create a Linked list after removing from list
l llarr:
ans checkpalindromel
if ans:
printtrue
else:
printfalse
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
