Created
March 27, 2017 17:33
-
-
Save sarath-soman/18265173c10f6c41fd0a1d493fa62194 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: | |
"""Node represents a single node of a linked list""" | |
def __init__(self, val): | |
self.val = val | |
self.next = None | |
def __str__(self): | |
return str(self.val) + " -> " + str(self.next) | |
class LinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
def add(self, val): | |
if(not self.head): | |
node = Node(val) | |
self.head = node | |
self.tail = node | |
else: | |
node = Node(val) | |
self.tail.next = node | |
self.tail = node | |
def __str__(self): | |
return str(self.head) | |
def main(): | |
list = LinkedList() | |
list.add(1) | |
list.add(2) | |
list.add(3) | |
print(list) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment