Created
April 21, 2015 07:53
-
-
Save yask123/18fa7ac115ca182ae2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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,data=0,next=None): | |
| self.data = data | |
| self.next = next | |
| class LinkedList: | |
| def __init__(self): | |
| self.head = None | |
| def push(self,data): | |
| new_node = Node() | |
| new_node.data = data | |
| new_node.next = self.head | |
| self.head = new_node | |
| def print_list(self): | |
| current = self.head | |
| while(current): | |
| print current.data | |
| current=current.next | |
| def reverse(self): | |
| current = self.head | |
| prev = None | |
| nex = None | |
| while current: | |
| nex = current.next | |
| current.next = prev | |
| prev = current | |
| current = nex | |
| self.head = prev | |
| ll = LinkedList() | |
| ll.push(1) | |
| ll.push(2) | |
| ll.push(3) | |
| ll.push(4) | |
| ll.print_list() | |
| ll.reverse() | |
| print 'Printing after reverse ' | |
| ll.print_list() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment