Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active June 20, 2020 05:45
Show Gist options
  • Save manojnaidu619/99aed24ab7eb747ecbf6a0d98e1a9143 to your computer and use it in GitHub Desktop.
Save manojnaidu619/99aed24ab7eb747ecbf6a0d98e1a9143 to your computer and use it in GitHub Desktop.
Linked list construction in python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def form_ll(self, length):
self.head = Node(1)
temp = self.head
for x in range(0, length-1):
temp.next = Node(x+2)
temp = temp.next
def display_ll(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
ll = LinkedList()
ll.form_ll(5) # Provide the length of linked list to be constructed.
ll. display_ll()
# ll.head -> gives access to head node
"""
Output :
1
2
3
4
5
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment