Created
August 1, 2012 08:44
-
-
Save gustavoatt/3225094 to your computer and use it in GitHub Desktop.
List reverse
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
#!/usr/bin/env python | |
class List(object): | |
def __init__(self): | |
self.val = None | |
self.next = None | |
def add(self, val): | |
current = self | |
while current.next is not None: | |
current = current.next | |
current.val = val | |
current.next = List() | |
def reverse(self): | |
current = self | |
previous = None | |
while current.next is not None: | |
following = current.next | |
current.next = previous | |
previous = current | |
current = following | |
return previous |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment