Palindrome LinkedList
You have been given a head to a singly linked list of integers. Write a function check to whether the list given is a 'Palindrome' or not.
Input format :
Remember/Consider :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
Explanation for the Sample Input 2 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self,data): | |
self.data=data | |
self.next=None | |
def LinkedList(arr): | |
head=None | |
tail=None | |
if len(arr)<1: | |
return | |
else: | |
for i in arr: | |
if i ==-1: | |
break | |
NewNode = Node(i) | |
if head == None: | |
head=NewNode | |
tail=NewNode | |
else: | |
tail.next=NewNode | |
tail=NewNode | |
return head | |
def printLL(head): | |
arr=[] | |
while head is not None: | |
arr.append(head.data) | |
head=head.next | |
if arr[0:]==arr[::-1]: | |
print('true') | |
else: | |
print('false') | |
t=int(input()) | |
for i in range(t): | |
arr=list(map(int,input().split())) | |
printLL(LinkedList(arr)) |
Comments
Post a Comment
Please give us your valuable feedback