fork download
  1. class Node:
  2. def __init__(self,value):
  3. self.data=value
  4. self.next=None
  5.  
  6. class Linkedlist:
  7. def __init__(self):
  8. self.head=None
  9. self.node=0
  10.  
  11. def reverse(self):
  12. curr=self.head
  13. prevNod=None
  14.  
  15. while curr != None:
  16. nextNod = curr.next
  17. curr.next=prevNod
  18. prevNod=curr
  19. curr=nextNod
  20.  
  21. self.head=prevNod
  22.  
  23. def insert_head(self,value):
  24. new_node=Node(value)
  25. new_node.next=self.head
  26. self.head=new_node
  27. self.node=self.node+1
  28.  
  29. L = Linkedlist()
  30. L.insert_head(4)
  31. L.insert_head(3)
  32. L.insert_head(2)
  33. L.insert_head(1)
  34. L.reverse()
  35. print(L)
Success #stdin #stdout 0.03s 9592KB
stdin
45
stdout
<__main__.Linkedlist object at 0x1516829a8dc0>