-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathll1.py
More file actions
37 lines (30 loc) · 910 Bytes
/
Copy pathll1.py
File metadata and controls
37 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
last = self.head
while last.next: # Traverse to the last node
last = last.next
last.next = new_node
def traverse(self):
"""Traverse and print all the elements in the linked list."""
current = self.head
while current: # Continue until the end of the list
print(current.data, end=" -> ")
current = current.next
print("None") # End of the list
if __name__ == "__main__":
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
print("Linked List Traversal:")
ll.traverse()