ascherj/linked-list-techniques
A Python implementation demonstrating three fundamental linked list techniques with clear examples and detailed explanations. Features Multiple Pass, Temporary Head, and Fast-Slow Pointer algorithms for common linked list operations including finding middle elements, reversing lists, and cycle detection.
Linked List Techniques Implementation
A comprehensive implementation of linked list data structures demonstrating three key algorithmic techniques: Multiple Pass, Slow-Fast Pointer, and Temporary Head.
๐ Quick Start
# Run all examples
python examples/demo_all.py
# Run specific technique demos
python examples/demo_multiple_pass.py
python examples/demo_slow_fast.py
python examples/demo_temporary_head.py
# Run all tests
python tests/run_all_tests.py๐ Project Structure
.
โโโ src/ # Source code
โ โโโ __init__.py # Package initialization
โ โโโ linked_list_base.py # Base Node and LinkedList classes
โ โโโ multiple_pass.py # Multiple pass technique implementation
โ โโโ slow_fast.py # Slow-fast pointer technique implementation
โ โโโ temporary_head.py # Temporary head technique implementation
โโโ tests/ # Unit tests
โ โโโ __init__.py # Test package initialization
โ โโโ test_linked_list_base.py # Base class tests (9 tests)
โ โโโ test_multiple_pass.py # Multiple pass tests (10 tests)
โ โโโ test_slow_fast.py # Slow-fast pointer tests (14 tests)
โ โโโ test_temporary_head.py # Temporary head tests (20 tests)
โ โโโ run_all_tests.py # Test runner (53 total tests)
โโโ examples/ # Demo scripts
โ โโโ demo_multiple_pass.py # Multiple pass technique demo
โ โโโ demo_slow_fast.py # Slow-fast pointer technique demo
โ โโโ demo_temporary_head.py # Temporary head technique demo
โ โโโ demo_all.py # Comprehensive demo
โโโ docs/ # Documentation
โ โโโ README_TESTS.md # Test documentation
โโโ README.md # This file
๐ฏ Techniques Implemented
1. Multiple Pass Technique
Purpose: Find the middle element of a linked list
Algorithm: Two-pass approach - count nodes, then traverse to middle
Time Complexity: O(n) | Space Complexity: O(1)
from src import MultiplePassLinkedList
llist = MultiplePassLinkedList()
for i in range(1, 6):
llist.append(i)
middle = llist.find_middle() # Returns 32. Slow-Fast Pointer Technique (Floyd's Algorithm)
Purpose: Detect cycles in linked lists
Algorithm: Two pointers moving at different speeds
Time Complexity: O(n) | Space Complexity: O(1)
from src import SlowFastLinkedList
llist = SlowFastLinkedList()
for i in range(1, 6):
llist.append(i)
llist.create_cycle(1) # Create cycle: 5 -> 2
cycle_start = llist.find_cycle_start() # Returns 23. Temporary Head Technique
Purpose: Simplify deletion and reversal operations
Algorithm: Use dummy node to handle edge cases
Time Complexity: O(n) | Space Complexity: O(1)
from src import TemporaryHeadLinkedList
llist = TemporaryHeadLinkedList()
for i in range(1, 6):
llist.append(i)
llist.delete_node(1) # Delete head - simplified with dummy node
llist.reverse() # Reverse list using temporary head๐งช Testing
The project includes comprehensive unit tests with 53 total tests covering:
- โ Core functionality and edge cases
- โ Algorithm correctness verification
- โ Different data types (integers, strings, mixed)
- โ Error handling and invalid inputs
- โ Performance characteristics
# Run all tests with detailed output
python tests/run_all_tests.py
# Run specific test modules
python -m unittest tests.test_multiple_pass -v
python -m unittest tests.test_slow_fast -v
python -m unittest tests.test_temporary_head -v๐ Performance Comparison
| Technique | Operation | Time | Space | Use Case |
|---|---|---|---|---|
| Multiple Pass | Find Middle | O(n) | O(1) | When you need exact middle |
| Slow-Fast | Cycle Detection | O(n) | O(1) | Detect loops/cycles |
| Slow-Fast | Find Middle | O(n) | O(1) | One-pass middle finding |
| Temporary Head | Deletion | O(n) | O(1) | Simplified edge cases |
| Temporary Head | Reversal | O(n) | O(1) | Clean reversal logic |
๐ง Usage Examples
Basic Usage
# Import all classes
from src import (
LinkedList,
MultiplePassLinkedList,
SlowFastLinkedList,
TemporaryHeadLinkedList
)
# Create and populate lists
llist = MultiplePassLinkedList()
for i in range(1, 6):
llist.append(i)
# Use specific techniques
middle = llist.find_middle()Advanced Usage
# Cycle detection example
cycle_list = SlowFastLinkedList()
for i in range(1, 8):
cycle_list.append(i)
cycle_list.create_cycle(2) # Create cycle at position 2
if cycle_list.find_cycle_start():
print("Cycle detected!")๐ Educational Value
This implementation demonstrates:
- Algorithm Design Patterns: Common techniques used in competitive programming
- Edge Case Handling: Proper handling of empty lists, single elements, etc.
- Code Organization: Clean separation of concerns and modular design
- Testing Practices: Comprehensive unit testing with edge cases
- Documentation: Clear explanations and usage examples
๐ค Contributing
- All code follows Python best practices
- Comprehensive tests are required for new features
- Documentation should be updated for any changes
- Examples should demonstrate real-world usage