Created
April 19, 2015 12:53
-
-
Save yask123/aceae3d2cda50740ec25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class node: | |
def __init__(self): | |
self.data = None | |
self.next = None | |
class linked_list: | |
def __init__(self): | |
self.current_node=None | |
def add_node(self,data): | |
new_node = node() | |
new_node.data = data | |
new_node.next = self.current_node | |
self.current_node = new_node | |
def list_print(self): | |
node = self.current_node | |
while node: | |
print node.data | |
node=node.next | |
def reverse(self): | |
current = self.current_node | |
prev = None | |
next = None | |
while current: | |
next = current.next | |
current.next = prev | |
prev = current | |
current = next | |
self.current_node = prev | |
def print_middle(self): | |
hare = self.current_node | |
tort=self.current_node | |
temp = tort | |
while( hare and hare.next ): | |
prev = tort | |
hare = hare.next.next | |
tort = tort.next | |
print tort.data | |
ll = linked_list() | |
ll.add_node(1) | |
ll.add_node(2) | |
ll.add_node(3) | |
ll.add_node(4) | |
ll.add_node(5) | |
ll.list_print() | |
print 'After reverse ' | |
ll.reverse() | |
ll.list_print() | |
print 'Middle value is ' | |
ll.print_middle() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment