Skip to content

Instantly share code, notes, and snippets.

@yask123
Created April 21, 2015 07:53
Show Gist options
  • Select an option

  • Save yask123/18fa7ac115ca182ae2c5 to your computer and use it in GitHub Desktop.

Select an option

Save yask123/18fa7ac115ca182ae2c5 to your computer and use it in GitHub Desktop.
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