<filename>03. Linked Lists/03-02 Linked List Reversal/reverse.py
from typing import Optional


class Node(object):
    def __init__(self, value):
        self.value = value
        self.nextnode = None


def reverse(head: Node) -> Optional[Node]:
    # initialize variables
    current_node = head
    prev_node: Optional[Node] = None
    next_node = None

    # while there are still nodes
    while current_node is not None:
        # save the node to visit next
        next_node = current_node.nextnode

        # replace next node with the previous one
        current_node.nextnode = prev_node

        # update the previous node as the current one
        prev_node = current_node
        current_node = next_node

    return prev_node

<filename>03. Linked Lists/03-02 Linked List Reversal/reverse.py
from typing import Optional


class Node(object):
    def __init__(self, value):
        self.value = value
        self.nextnode = None


def reverse(head: Node) -> Optional[Node]:
    # initialize variables
    current_node = head
    prev_node: Optional[Node] = None
    next_node = None

    # while there are still nodes
    while current_node is not None:
        # save the node to visit next
        next_node = current_node.nextnode

        # replace next node with the previous one
        current_node.nextnode = prev_node

        # update the previous node as the current one
        prev_node = current_node
        current_node = next_node

    return prev_node

# initialize variables # while there are still nodes # save the node to visit next # replace next node with the previous one # update the previous node as the current one