data_structures.linked_list.is_palindrome¶
Classes¶
Functions¶
|
Check if a linked list is a palindrome. |
|
Check if a linked list is a palindrome using a dictionary. |
|
Check if a linked list is a palindrome using a stack. |
Module Contents¶
- data_structures.linked_list.is_palindrome.is_palindrome(head: ListNode | None) bool ¶
Check if a linked list is a palindrome.
- Args:
head: The head of the linked list.
- Returns:
bool: True if the linked list is a palindrome, False otherwise.
- Examples:
>>> is_palindrome(None) True
>>> is_palindrome(ListNode(1)) True
>>> is_palindrome(ListNode(1, ListNode(2))) False
>>> is_palindrome(ListNode(1, ListNode(2, ListNode(1)))) True
>>> is_palindrome(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True
- data_structures.linked_list.is_palindrome.is_palindrome_dict(head: ListNode | None) bool ¶
Check if a linked list is a palindrome using a dictionary.
- Args:
head (ListNode): The head of the linked list.
- Returns:
bool: True if the linked list is a palindrome, False otherwise.
- Examples:
>>> is_palindrome_dict(None) True
>>> is_palindrome_dict(ListNode(1)) True
>>> is_palindrome_dict(ListNode(1, ListNode(2))) False
>>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(1)))) True
>>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True
>>> is_palindrome_dict( ... ListNode( ... 1, ListNode(2, ListNode(1, ListNode(3, ListNode(2, ListNode(1))))) ... ) ... ) False
- data_structures.linked_list.is_palindrome.is_palindrome_stack(head: ListNode | None) bool ¶
Check if a linked list is a palindrome using a stack.
- Args:
head (ListNode): The head of the linked list.
- Returns:
bool: True if the linked list is a palindrome, False otherwise.
- Examples:
>>> is_palindrome_stack(None) True
>>> is_palindrome_stack(ListNode(1)) True
>>> is_palindrome_stack(ListNode(1, ListNode(2))) False
>>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(1)))) True
>>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(2, ListNode(1))))) True