Question: class Node: def _ _ init _ _ ( self , data ) : self.data = data self.next = None def length ( head )

class Node:
def __init__(self, data):
self.data = data
self.next = None
def length(head):
count =0
while head:
count +=1
head = head.next
return count
def check_palindrome(head):
l = length(head)
t1= head
mid = l //2
is_palindrome = True
# Traverse to the midpoint
for i in range(mid):
t1= t1.next
# Reverse the second half of the list
prev = None
while t1:
next_node = t1.next
t1.next = prev
prev = t1
t1= next_node
# Compare the first half and the reversed second half
t1= head
t2= prev
for i in range(mid):
if t1.data != t2.data:
is_palindrome = False
break
t1= t1.next
t2= t2.next
return is_palindrome
def ll(arr):
if len(arr)==0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
# Main
# Read the linked list elements including -1
arr = list(int(i) for i in input().strip().split(''))
# Create a Linked list after removing -1 from list
l = ll(arr[:-1])
ans = check_palindrome(l)
if ans:
print("true")
else:
print("false")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!