Reputation: 2045
I know the definition of linked list...each unit have data and point to the next...but how did it implement in python? I am really confused about that...
For example, compare with list in python, if I want to create a list:
l1 = [1, 2, 3]
if I want to add element to this list, just use append()
or insert()
it is very easy
However, many material said it is need to create a class if you want make a linked list like:
class ListNode:
def __init__(self, data):
self.data = data
self.next = None
return
def has_value(self, value):
if self.data == value:
return True
else:
return False
node1 = ListNode(2)
node2 = ListNode(1.2)
node3 = ListNode('a')
I was wondering if there is some way can make it easier like:
(1.2) = (2).next
('a') = (1.2).next
Besides, I find some methods likne .val .next .head
, when can it be used?
Upvotes: 0
Views: 200
Reputation: 51
You are confusing Python lists with linked lists. They are not the same. The one you mention first is a Python list. And the one you mention with a Class, is a Linked List.
LISTS :
LINKED LISTS :
Refer this for more information on linked lists.
Feel free to comment if you have any queries.
Upvotes: 1