data_structures.linked_list.is_palindrome

Classes

ListNode

Functions

is_palindrome(→ bool)

Check if a linked list is a palindrome.

is_palindrome_dict(→ bool)

Check if a linked list is a palindrome using a dictionary.

is_palindrome_stack(→ bool)

Check if a linked list is a palindrome using a stack.

Module Contents

class data_structures.linked_list.is_palindrome.ListNode
next_node: ListNode | None = None
val: int = 0
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