m2skills

reverse Linked List PYTHON

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