print the entire linked list iteratively this function prints the elements of a linked list separated by parameters head node none the head of the linked list to be printed or none if the linked list is empty head insertnodenone 0 head insertnodehead 2 head insertnodehead 1 printlinkedlisthead 021 head insertnodehead 4 head insertnodehead 5 printlinkedlisthead 02145 insert a new node at the end of a linked list and return the new head parameters head node none the head of the linked list data int the data to be inserted into the new node returns node the new head of the linked list head insertnodenone 10 head insertnodehead 9 head insertnodehead 8 printlinkedlisthead 1098 if the linked list is empty the newnode becomes the head rotate a linked list to the right by places times parameters head the head of the linked list places the number of places to rotate returns node the head of the rotated linked list rotatetotherightnone places1 traceback most recent call last valueerror the linked list is empty head insertnodenone 1 rotatetotherighthead places1 head true head insertnodenone 1 head insertnodehead 2 head insertnodehead 3 head insertnodehead 4 head insertnodehead 5 newhead rotatetotherighthead places2 printlinkedlistnewhead 45123 check if the list is empty or has only one element calculate the length of the linked list adjust the value of places to avoid places longer than the list find the new head position after rotation traverse to the new head position update pointers to perform rotation print the entire linked list iteratively this function prints the elements of a linked list separated by parameters head node none the head of the linked list to be printed or none if the linked list is empty head insert_node none 0 head insert_node head 2 head insert_node head 1 print_linked_list head 0 2 1 head insert_node head 4 head insert_node head 5 print_linked_list head 0 2 1 4 5 insert a new node at the end of a linked list and return the new head parameters head node none the head of the linked list data int the data to be inserted into the new node returns node the new head of the linked list head insert_node none 10 head insert_node head 9 head insert_node head 8 print_linked_list head 10 9 8 if the linked list is empty the new_node becomes the head type ignore rotate a linked list to the right by places times parameters head the head of the linked list places the number of places to rotate returns node the head of the rotated linked list rotate_to_the_right none places 1 traceback most recent call last valueerror the linked list is empty head insert_node none 1 rotate_to_the_right head places 1 head true head insert_node none 1 head insert_node head 2 head insert_node head 3 head insert_node head 4 head insert_node head 5 new_head rotate_to_the_right head places 2 print_linked_list new_head 4 5 1 2 3 check if the list is empty or has only one element calculate the length of the linked list adjust the value of places to avoid places longer than the list as no rotation is needed find the new head position after rotation traverse to the new head position update pointers to perform rotation
from __future__ import annotations

from dataclasses import dataclass


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


def print_linked_list(head: Node | None) -> None:
    
    if head is None:
        return
    while head.next_node is not None:
        print(head.data, end="->")
        head = head.next_node
    print(head.data)


def insert_node(head: Node | None, data: int) -> Node:
    
    new_node = Node(data)
    
    if head is None:
        return new_node

    temp_node = head
    while temp_node.next_node:
        temp_node = temp_node.next_node

    temp_node.next_node = new_node  
    return head


def rotate_to_the_right(head: Node, places: int) -> Node:
    
    
    if not head:
        raise ValueError("The linked list is empty.")

    if head.next_node is None:
        return head

    
    length = 1
    temp_node = head
    while temp_node.next_node is not None:
        length += 1
        temp_node = temp_node.next_node

    
    places %= length

    if places == 0:
        return head  

    
    new_head_index = length - places

    
    temp_node = head
    for _ in range(new_head_index - 1):
        assert temp_node.next_node
        temp_node = temp_node.next_node

    
    assert temp_node.next_node
    new_head = temp_node.next_node
    temp_node.next_node = None
    temp_node = new_head
    while temp_node.next_node:
        temp_node = temp_node.next_node
    temp_node.next_node = head

    assert new_head
    return new_head


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    head = insert_node(None, 5)
    head = insert_node(head, 1)
    head = insert_node(head, 2)
    head = insert_node(head, 4)
    head = insert_node(head, 3)

    print("Original list: ", end="")
    print_linked_list(head)

    places = 3
    new_head = rotate_to_the_right(head, places)

    print(f"After {places} iterations: ", end="")
    print_linked_list(new_head)