0% found this document useful (0 votes)
2 views1 page

Python3linked List - Py

The document defines a LinkedList class for creating and managing a linked list structure. It includes methods for inserting nodes at the head of the list and creating a linked list from an integer array. Additionally, it provides a string representation of the linked list elements.

Uploaded by

xyza04481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Python3linked List - Py

The document defines a LinkedList class for creating and managing a linked list structure. It includes methods for inserting nodes at the head of the list and creating a linked list from an integer array. Additionally, it provides a string representation of the linked list elements.

Uploaded by

xyza04481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

from linked_list_node import LinkedListNode

# Template for the linked list


class LinkedList:
# __init__ will be used to make a LinkedList type object.
def __init__(self):
self.head = None

# insert_node_at_head method will insert a LinkedListNode at head


# of a linked list.
def insert_node_at_head(self, node):
if self.head:
node.next = self.head
self.head = node
else:
self.head = node

# create_linked_list method will create the linked list using the


# given integer array with the help of InsertAthead method.
def create_linked_list(self, lst):
for x in reversed(lst):
new_node = LinkedListNode(x)
self.insert_node_at_head(new_node)

# __str__(self) method will display the elements of linked list.


def __str__(self):
result = ""
temp = self.head
while temp:
result += str(temp.data)
temp = temp.next
if temp:
result += ", "
result += ""
return result

You might also like