Skip to content

Instantly share code, notes, and snippets.

@sarath-soman
Created March 27, 2017 17:33
Show Gist options
  • Save sarath-soman/18265173c10f6c41fd0a1d493fa62194 to your computer and use it in GitHub Desktop.
Save sarath-soman/18265173c10f6c41fd0a1d493fa62194 to your computer and use it in GitHub Desktop.
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