Linked List¶
py_ds.datastructures.linked_lists.singly_linked.LinkedList
¶
Bases: Generic[T]
A singly linked list supporting typical operations.
Supports append/prepend, insert at index, remove by value, iteration, and length/truthiness operations.
Initialize the list with optional items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[T] | None
|
Optional iterable of items to initialize the list with. If None, creates an empty list. |
None
|
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
Functions¶
append
¶
Add a value to the end of the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to append to the list. |
required |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
prepend
¶
Add a value to the beginning of the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to prepend to the list. |
required |
Time complexity: O(1).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
insert
¶
Insert a value at a specific index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
0-based index, negative indexes supported (Python style). |
required |
value
|
T
|
The value to insert. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If index is out of bounds. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
remove
¶
Remove the first occurrence of value from the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to remove from the list. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not found. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
pop
¶
Remove and return the item at the given index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
0-based index, negative indexes supported (Python style). Defaults to -1 (last element). |
-1
|
Returns:
| Type | Description |
|---|---|
T
|
The value at the specified index. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the list is empty or index is invalid. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
clear
¶
head
¶
Return the first value in the list.
Returns:
| Type | Description |
|---|---|
T | None
|
The first value in the list, or None if the list is empty. |
Time complexity: O(1).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
tail
¶
Return the last value in the list.
Returns:
| Type | Description |
|---|---|
T | None
|
The last value in the list, or None if the list is empty. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
find
¶
Return the index of the first occurrence of a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to search for. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The index of the first occurrence of the value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is not found in the list. |
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
__len__
¶
Return the number of elements in the list.
Returns:
| Type | Description |
|---|---|
int
|
The number of elements in the linked list. |
__bool__
¶
Return the truthiness of the list.
Returns:
| Type | Description |
|---|---|
bool
|
False if the list is empty, True otherwise. |
__getitem__
¶
Get the value at the given index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
0-based index, negative indexes supported (Python style). |
required |
Returns:
| Type | Description |
|---|---|
T
|
The value at the specified index. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the index is out of range. |
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
__setitem__
¶
Set item at the specified index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The position at which to set the value. 0-based index, negative indexes supported (Python style). |
required |
value
|
T
|
The value to set. |
required |
Raises:
| Type | Description |
|---|---|
IndexError
|
If index is out of bounds. |
Time complexity: O(n).
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
__iter__
¶
Iterate through values in the list.
Yields:
| Type | Description |
|---|---|
T
|
The values in the list from head to tail. |
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
__repr__
¶
Return a string representation of the linked list.
Returns:
| Type | Description |
|---|---|
str
|
A string representation showing the class name and list contents. |
Source code in src/py_ds/datastructures/linked_lists/singly_linked.py
__str__
¶
Return a string representation of the linked list.
Returns:
| Type | Description |
|---|---|
str
|
A visual representation of the linked list. |
Time complexity: O(n).