from dataclasses import dataclass


@dataclass(eq=False)
class Node:
    """One node in a singly linked integer list.

    Attributes:
        value: The integer stored at this node.
        next: The following node, or None at the end of the list.
    """

    value: int
    next: "Node | None" = None


def make_list(values: list[int]) -> Node | None:
    """Build a linked list in the same order as the input values.

    Args:
        values: Integers to place in newly allocated nodes.

    Returns:
        The first node, or None for an empty input.
    """
    head = None
    for value in reversed(values):
        head = Node(value, head)
    return head


def list_values(head: Node | None) -> list[int]:
    """Collect values from an acyclic singly linked list.

    Args:
        head: The first node, or None for an empty list.

    Returns:
        Node values in traversal order.
    """
    values = []
    current = head
    while current is not None:
        values.append(current.value)
        current = current.next
    return values


head = make_list([4, 7, 9])
print("Traversal:", list_values(head))
print("Empty traversal:", list_values(None))

def reverse_list(head: Node | None) -> Node | None:
    """Reverse an acyclic singly linked list by changing its links.

    Args:
        head: The first node of the list to modify, or None.

    Returns:
        The first node after reversal, or None for an empty list.
    """
    previous = None
    current = head
    while current is not None:
        following = current.next
        current.next = previous
        previous = current
        current = following
    return previous


current = make_list([4, 7, 9])
previous = None
print("Initial reversed:", list_values(previous))
print("Initial untouched:", list_values(current))
while current is not None:
    following = current.next
    current.next = previous
    previous = current
    current = following
    print("Reversed:", list_values(previous))
    print("Untouched:", list_values(current))

head = make_list([4, 7, 9])
old_head = head
head = reverse_list(head)
print("Reversed traversal:", list_values(head))
print("Traversal from old head:", list_values(old_head))

assert reverse_list(None) is None
single = Node(6)
assert reverse_list(single) is single
assert single.next is None
head = make_list([2, 2, 5])
assert list_values(reverse_list(head)) == [5, 2, 2]
print("Boundary checks passed.")
