m2skills

isPalindrome LL PYTHON

Jan 28th, 2018
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. # program to check if the string or number represented by linked list is palindrome or not
  2.  
  3. class LinkedList:
  4.     def __init__(self):
  5.         self.head = None
  6.  
  7.     # returns True is LinkedList is Empty
  8.     def isEmpty(self):
  9.         if self.head is None:
  10.             return True
  11.         else:
  12.             return False
  13.  
  14.     # method adds elements to the left of the Linked List
  15.     def addToStart(self, data):
  16.         # create a temporary node
  17.         tempNode = Node(data)
  18.         tempNode.setLink(self.head)
  19.         self.head = tempNode
  20.         del tempNode
  21.  
  22.     # method adds elements to the right of the Linked List
  23.     def addToEnd(self, data):
  24.         start = self.head
  25.         tempNode = Node(data)
  26.         while start.getNextNode():
  27.             start = start.getNextNode()
  28.         start.setLink(tempNode)
  29.         del tempNode
  30.         return True
  31.  
  32.     # method displays Linked List
  33.     def display(self):
  34.         start = self.head
  35.         if start is None:
  36.             print("Empty List!!!")
  37.             return False
  38.  
  39.         while start:
  40.             print(str(start.getData()), end=" ")
  41.             start = start.link
  42.             if start:
  43.                 print("-->", end=" ")
  44.         print()
  45.  
  46. # node class
  47. class Node:
  48.     # default value of data and link is none if no data is passed
  49.     def __init__(self, data=None, link=None):
  50.         self.data = data
  51.         self.link = link
  52.  
  53.     # method to update the data feild of Node
  54.     def updateData(self, data):
  55.         self.data = data
  56.  
  57.     # method to set Link feild the Node
  58.     def setLink(self, node):
  59.         self.link = node
  60.  
  61.     # method returns data feild of the Node
  62.     def getData(self):
  63.         return self.data
  64.  
  65.     # method returns address of the next Node
  66.     def getNextNode(self):
  67.         return self.link
  68.  
  69. # checking if linked list is a palindrome and returns a boolean
  70. def isPalindrome(right, left):
  71.     isPalindrome.left = left
  72.     if right is None:
  73.         return True
  74.  
  75.     # moving right node to the end of linked list
  76.     if right.getNextNode() is not None:
  77.         right = right.getNextNode()
  78.         isPalindrome(right, isPalindrome.left)
  79.  
  80.     # comparing left and right nodes
  81.     if isPalindrome.left.getData() == right.getData():
  82.         isPalindrome.left = isPalindrome.left.getNextNode()
  83.         return True
  84.  
  85.     return False
  86.  
  87.  
  88. # main method
  89. print("program to check if the given Linked List is a Palindrome or not")
  90. # creating LinkedList
  91. myList = LinkedList()
  92. myList.addToStart(1)
  93. myList.addToEnd(2)
  94. myList.addToEnd(3)
  95. myList.addToEnd(4)
  96. myList.addToEnd(5)
  97. myList.addToEnd(6)
  98. myList.addToEnd(6)
  99. myList.addToEnd(4)
  100. myList.addToEnd(3)
  101. myList.addToEnd(2)
  102. myList.addToEnd(1)
  103. myList.display()
  104.  
  105. print("Is the Linked List a Palindrome : " + str(isPalindrome(myList.head, myList.head)))
Add Comment
Please, Sign In to add comment