Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, data = None, next = None):
- self.data = data
- self.next = next
- def print_to_screen(head):
- if head != None:
- print(head.data, end=" ")
- print_to_screen(head.next)
- else:
- print("")
- # def copy_list(head):
- # if head == None:
- # return None
- # return Node(head.data, copy_list(head.next))
- # def reverse_list(head):
- # if head == None or head.next == None:
- # return head
- # node = reverse_list(head.next)
- # head.next.next = head
- # head.next = None
- # return node
- # def compare_list(head1, head2):
- # if head1 == None or head2 == None:
- # return True
- # elif head1 == None or head2 == None:
- # return False
- # elif head1.data == head2.data:
- # return compare_list(head1.next, head2.next)
- # return False
- # def palindrome(head):
- # head2 = copy_list(head)
- # head2 = reverse_list(head2)
- # return compare_list(head, head2)
- class Palindrome:
- def __init__(self,head):
- self.head = head
- def palindrome(self, tail):
- if tail == None:
- return True
- if self.palindrome(tail.next):
- if self.head.data == tail.data:
- self.head = self.head.next
- return True
- return False
- def palindrome(head):
- pal = Palindrome(head)
- return pal.palindrome(head)
- if __name__ == "__main__":
- print("\n")
- head = Node("A", Node("E", Node("L", Node("E", Node("A", None)))))
- print_to_screen(head)
- print(palindrome(head))
- print_to_screen(head)
- print("\n")
- head = Node("A", Node("E", Node("L", Node("B", Node("A", None)))))
- print_to_screen(head)
- print(palindrome(head))
- print_to_screen(head)
- print("\n")
- head = Node("C", Node("A", Node("L", Node("L", Node("A", Node("C", None))))))
- print_to_screen(head)
- print(palindrome(head))
- print_to_screen(head)
- print("\n")
- head = Node("C", Node("A", Node("L", Node("T", Node("E", Node("C", None))))))
- print_to_screen(head)
- print(palindrome(head))
- print_to_screen(head)
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement