Created
March 4, 2014 08:01
-
-
Save barnash/9342109 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, val): | |
self.value = val | |
self.next = None | |
def __repr__(self): | |
return "[" + str(self.value) + "," + str(id(self.next)) + "]" | |
class LinkedList(): | |
def __init__(self): | |
self.first = None | |
self.size = 0 | |
def add_at_start(self, val): | |
prev_first = self.first | |
self.first = Node(val) | |
self.first.next = prev_first | |
self.size += 1 | |
def reverse(self): | |
prev = None | |
curr = self.first | |
while curr != None: | |
tmp = curr.next | |
curr.next = prev | |
prev = curr | |
curr = tmp | |
self.first = prev | |
# a -> b -> c | |
# a <- b -< c | |
def matan_print(self): | |
curr = self.first | |
while curr != None: | |
print(curr.value) | |
curr = curr.next | |
def string_to_linked_list(st): | |
l = LinkedList() | |
for ch in st[::-1]: | |
l.add_at_start(ch) | |
return l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment