a class to represent a linked list use a tail pointer to speed up the append operation initialize a linkedlist with the head node set to none linkedlist linkedlist linkedlist head linkedlist tail none none iterate the linkedlist yielding each node s data linkedlist linkedlist items 1 2 3 4 5 linkedlist extenditems tuplelinkedlist items true returns a string representation of the linkedlist linkedlist linkedlist strlinkedlist linkedlist append1 strlinkedlist 1 linkedlist extend2 3 4 5 strlinkedlist 1 2 3 4 5 appends a new node with the given data to the end of the linkedlist linkedlist linkedlist strlinkedlist linkedlist append1 strlinkedlist 1 linkedlist append2 strlinkedlist 1 2 appends each item to the end of the linkedlist linkedlist linkedlist linkedlist extend strlinkedlist linkedlist extend1 2 strlinkedlist 1 2 linkedlist extend3 4 strlinkedlist 1 2 3 4 creates a linked list from the elements of the given sequence listtuple and returns the head of the linked list makelinkedlist traceback most recent call last exception the elements list is empty makelinkedlist7 7 makelinkedlist abc abc makelinkedlist7 25 7 25 prints the elements of the given linked list in reverse order inreverselinkedlist inreversemakelinkedlist69 88 73 73 88 69 a class to represent a linked list use a tail pointer to speed up the append operation initialize a linkedlist with the head node set to none linked_list linkedlist linked_list head linked_list tail none none speeds up the append operation iterate the linkedlist yielding each node s data linked_list linkedlist items 1 2 3 4 5 linked_list extend items tuple linked_list items true returns a string representation of the linkedlist linked_list linkedlist str linked_list linked_list append 1 str linked_list 1 linked_list extend 2 3 4 5 str linked_list 1 2 3 4 5 appends a new node with the given data to the end of the linkedlist linked_list linkedlist str linked_list linked_list append 1 str linked_list 1 linked_list append 2 str linked_list 1 2 appends each item to the end of the linkedlist linked_list linkedlist linked_list extend str linked_list linked_list extend 1 2 str linked_list 1 2 linked_list extend 3 4 str linked_list 1 2 3 4 creates a linked list from the elements of the given sequence list tuple and returns the head of the linked list make_linked_list traceback most recent call last exception the elements list is empty make_linked_list 7 7 make_linked_list abc abc make_linked_list 7 25 7 25 prints the elements of the given linked list in reverse order in_reverse linkedlist in_reverse make_linked_list 69 88 73 73 88 69
from __future__ import annotations

from collections.abc import Iterable, Iterator
from dataclasses import dataclass


@dataclass
class Node:
    data: int
    next_node: Node | None = None


class LinkedList:
    

    def __init__(self) -> None:
        
        self.head: Node | None = None
        self.tail: Node | None = None  

    def __iter__(self) -> Iterator[int]:
        
        node = self.head
        while node:
            yield node.data
            node = node.next_node

    def __repr__(self) -> str:
        
        return " -> ".join([str(data) for data in self])

    def append(self, data: int) -> None:
        
        if self.tail:
            self.tail.next_node = self.tail = Node(data)
        else:
            self.head = self.tail = Node(data)

    def extend(self, items: Iterable[int]) -> None:
        
        for item in items:
            self.append(item)


def make_linked_list(elements_list: Iterable[int]) -> LinkedList:
    
    if not elements_list:
        raise Exception("The Elements List is empty")

    linked_list = LinkedList()
    linked_list.extend(elements_list)
    return linked_list


def in_reverse(linked_list: LinkedList) -> str:
    
    return " <- ".join(str(line) for line in reversed(tuple(linked_list)))


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    linked_list = make_linked_list((14, 52, 14, 12, 43))
    print(f"Linked List:  {linked_list}")
    print(f"Reverse List: {in_reverse(linked_list)}")