-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.py
60 lines (47 loc) · 1.39 KB
/
LinkedList.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from typing import Iterable
class Node:
"""
Node Class for the nodes of a Linked List
"""
def __init__(self, val: int = None) -> None:
self.val = val
self.next = None
def __repr__(self) -> str:
if self.next:
return f"{str(self.val)} => {str(self.next)}"
return str(self.val)
class LinkedList:
"""
Linked List Class
Functions:
add: function to add a node at the end of the linked list
"""
def __init__(self) -> None:
self.head = None
self.rear = None
self.length = 0
def __repr__(self) -> str:
return str(self.head)
def add(self, val: int = 0):
# Add a new node with the provided value and adds it at the rear of the list
self.length += 1
if self.head is None:
self.head = Node(val)
self.rear = self.head
else:
self.rear.next = Node(val)
self.rear = self.rear.next
def __len__(self) -> int:
return self.length
# iteration initialization
def __iter__(self) -> Iterable:
self.curr = self.head
return self
# next function to iterate through the linked list iterator
def __next__(self) -> int:
if self.curr:
value = self.curr.val
self.curr = self.curr.next
return value
else:
raise StopIteration