data_structures.linked_list.rotate_to_the_right¶
Attributes¶
Classes¶
Functions¶
|
Insert a new node at the end of a linked list and return the new head. |
|
Print the entire linked list iteratively. |
|
Rotate a linked list to the right by places times. |
Module Contents¶
- data_structures.linked_list.rotate_to_the_right.insert_node(head: Node | None, data: int) Node ¶
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
- data_structures.linked_list.rotate_to_the_right.print_linked_list(head: Node | None) None ¶
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
- data_structures.linked_list.rotate_to_the_right.rotate_to_the_right(head: Node, places: int) Node ¶
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
- data_structures.linked_list.rotate_to_the_right.head¶