Question: Please do this in python. Debugging- Palindrome LinkedList You have been given a problem which has a solution to it already. You need to find
Please do this in python.
Debugging- Palindrome LinkedList
You have been given a problem which has a solution to it already. You need to find out the bug/bugs(if any)
Check if a given linked list is palindrome or not. Return true or false.
Indexing starts from 0.
Input format : Linked list elements (separated by space and terminated by -1)
Sample Input 1 : 9 2 3 3 2 9 -1
Sample Output 1 : true
Sample Input 2 : 0 2 3 2 5 -1
Sample Output 2 : false
CODE--->
```
class Node: def __init__(self, data): self.data = data self.next = None
def check_palindrome(head) : l = length(head) t1 = head mid= l/2 isPalindrome = True for i in range(mid) : t1= t1.next;
node* t2 = None t2= t1.next t1.next= None
while t2.next!=None : if t2.data == t1.data : isPalindrome=True else : isPalindrome= False
t1=t1.next t2=t2.next
return isPalindrome
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 link 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")
```
This is how code looks in editor:

PLEASE DO THIS IN PYTHON and if possible write what is wrong in comment inside the code.
1 ## Read input as specified in the question. 2 ## Print output as specified in the question. 3 4 class Node: 5 def _init__(self, data): 6 self.data = data 7 self.next = None 8 9 def check_palindrome(head): 10 I = length(head) 11 t1 = head 12 mid=1/2 13 isPalindrome = True 14 15 for i in range(mid): 16 t1=t1.next; 17 18 node* t2 = None 19 t2=t1.next 20 t1.next= None 21 22 while t2.next!=None : 23 if t2.data == t1.data: 24 isPalindrome=True 25 else: 26 isPalindrome= False 27 28 t1=t1.next 29 t2=t2.next 30 31 return is Palindrome 32 33 def llarr): 34 if len(arr)==0: 35 return None 36 head = Node (arr[0]) 37 last = head 38 for data in arr[1:): 39 last.next = Node(data) 40 last = last.next 41 return head 42 43 # Main 44 # Read the link list elements including -1 45 arr=list(int(i) fori in input().strip().split(' ')) 46 # Create a Linked list after removing -1 from list 47 |= Illarr[:-1]) 48 ans =check_palindrome(1) 49 if ans: 50 print("true") 51 else: 52 print("false")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
