Last active
June 20, 2020 05:45
-
-
Save manojnaidu619/99aed24ab7eb747ecbf6a0d98e1a9143 to your computer and use it in GitHub Desktop.
Linked list construction in python
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: | |
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