Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. class Node:
  2. def __init__(self, data = None, next = None):
  3. self.data = data
  4. self.next = next
  5.  
  6. def print_to_screen(head):
  7. if head != None:
  8. print(head.data, end=" ")
  9. print_to_screen(head.next)
  10. else:
  11. print("")
  12.  
  13. # def copy_list(head):
  14. # if head == None:
  15. # return None
  16. # return Node(head.data, copy_list(head.next))
  17.  
  18. # def reverse_list(head):
  19. # if head == None or head.next == None:
  20. # return head
  21. # node = reverse_list(head.next)
  22. # head.next.next = head
  23. # head.next = None
  24. # return node
  25.  
  26. # def compare_list(head1, head2):
  27. # if head1 == None or head2 == None:
  28. # return True
  29. # elif head1 == None or head2 == None:
  30. # return False
  31. # elif head1.data == head2.data:
  32. # return compare_list(head1.next, head2.next)
  33. # return False
  34.  
  35. # def palindrome(head):
  36. # head2 = copy_list(head)
  37. # head2 = reverse_list(head2)
  38. # return compare_list(head, head2)
  39.  
  40. class Palindrome:
  41. def __init__(self,head):
  42. self.head = head
  43.  
  44. def palindrome(self, tail):
  45. if tail == None:
  46. return True
  47.  
  48. if self.palindrome(tail.next):
  49. if self.head.data == tail.data:
  50. self.head = self.head.next
  51. return True
  52. return False
  53.  
  54. def palindrome(head):
  55. pal = Palindrome(head)
  56. return pal.palindrome(head)
  57.  
  58.  
  59.  
  60. if __name__ == "__main__":
  61.  
  62. print("\n")
  63. head = Node("A", Node("E", Node("L", Node("E", Node("A", None)))))
  64. print_to_screen(head)
  65. print(palindrome(head))
  66. print_to_screen(head)
  67.  
  68. print("\n")
  69.  
  70. head = Node("A", Node("E", Node("L", Node("B", Node("A", None)))))
  71. print_to_screen(head)
  72. print(palindrome(head))
  73. print_to_screen(head)
  74.  
  75. print("\n")
  76.  
  77. head = Node("C", Node("A", Node("L", Node("L", Node("A", Node("C", None))))))
  78. print_to_screen(head)
  79. print(palindrome(head))
  80. print_to_screen(head)
  81.  
  82. print("\n")
  83.  
  84. head = Node("C", Node("A", Node("L", Node("T", Node("E", Node("C", None))))))
  85. print_to_screen(head)
  86. print(palindrome(head))
  87. print_to_screen(head)
  88.  
  89. print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement