From 6e610d4782011f92c1886908e61a91652d9c8184 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 16:17:05 +0500 Subject: [PATCH 01/14] Rotate linked list by k. --- .../linked_list/rotate_linked_list_by_k.py | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 data_structures/linked_list/rotate_linked_list_by_k.py diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py new file mode 100644 index 000000000000..d39b0d3f775f --- /dev/null +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -0,0 +1,142 @@ +from __future__ import annotations + +class Node: + def __init__(self, data: int) -> None: + self.data = data + self.next = None + + +def print_linked_list(head: Node) -> None: + """ + Print the entire linked list iteratively. + + Parameters: + head (Node): The head of the linked list to be printed. + + >>> head = None + >>> head = insert_node(head, 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 + """ + if head is None: + return + while head.next is not None: + print(head.data, end="->") + head = head.next + print(head.data) + + +def insert_node(head: Node, data: int) -> Node: + """ + Insert a new node at the end of a linked list and return the new head. + + Parameters: + head (Node): 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 = None + >>> head = insert_node(head, 10) + >>> head = insert_node(head, 9) + >>> head = insert_node(head, 8) + >>> print_linked_list(head) + 10->9->8 + """ + new_node = Node(data) + # If the linked list is empty, the new_node becomes the head + if head is None: + return new_node + + temp_node = head + while temp_node.next is not None: + temp_node = temp_node.next + + temp_node.next = new_node + return head + + +def right_rotate_by_k(head: Node, k: int) -> Node: + """ + Rotate a linked list to the right by k times. + + Parameters: + head (Node): The head of the linked list. + k (int): The number of places to rotate. + + Returns: + Node: The head of the rotated linked list. + + >>> head = None + >>> head = insert_node(head, 1) + >>> head = insert_node(head, 2) + >>> head = insert_node(head, 3) + >>> head = insert_node(head, 4) + >>> head = insert_node(head, 5) + >>> k = 2 + >>> new_head = right_rotate_by_k(head, k) + >>> print_linked_list(new_head) + 4->5->1->2->3 + """ + # Check if the list is empty or has only one element + if head is None or head.next is None: + return head + + # Calculate the length of the linked list + length = 1 + temp_node = head + while temp_node.next is not None: + length += 1 + temp_node = temp_node.next + + # Adjust the value of k to avoid unnecessary rotations. + k = k % length + + if k == 0: + return head #As No rotation needed. + + # Find the new head position after rotation. + new_head_index = length - k + + # Traverse to the new head position + temp_node = head + for _ in range(new_head_index - 1): + temp_node = temp_node.next + + # Update pointers to perform rotation + new_head = temp_node.next + temp_node.next = None + temp_node = new_head + while temp_node.next is not None: + temp_node = temp_node.next + temp_node.next = head + + return new_head + +if __name__ == "__main__": + import doctest + + doctest.testmod() + head = None + + head = insert_node(head, 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) + + k = 3 + new_head = right_rotate_by_k(head, k) + + print("After", k, "iterations: ", end="") + print_linked_list(new_head) \ No newline at end of file From 6122dede1e637d2cc036666de0120198145cbb72 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 16:29:56 +0500 Subject: [PATCH 02/14] Rotate linked list by k. --- .../linked_list/rotate_linked_list_by_k.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index d39b0d3f775f..809d9b83afab 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -1,5 +1,6 @@ from __future__ import annotations + class Node: def __init__(self, data: int) -> None: self.data = data @@ -12,7 +13,7 @@ def print_linked_list(head: Node) -> None: Parameters: head (Node): The head of the linked list to be printed. - + >>> head = None >>> head = insert_node(head, 0) >>> head = insert_node(head, 2) @@ -100,7 +101,7 @@ def right_rotate_by_k(head: Node, k: int) -> Node: k = k % length if k == 0: - return head #As No rotation needed. + return head # As No rotation needed. # Find the new head position after rotation. new_head_index = length - k @@ -120,17 +121,18 @@ def right_rotate_by_k(head: Node, k: int) -> Node: return new_head + if __name__ == "__main__": - import doctest + import doctest doctest.testmod() head = None - head = insert_node(head, 5) - head = insert_node(head, 1) + # head = insert_node(head, 5) + # head = insert_node(head, 1) head = insert_node(head, 2) head = insert_node(head, 4) - head = insert_node(head, 3) + # head = insert_node(head, 3) print("Original list: ", end="") print_linked_list(head) @@ -139,4 +141,4 @@ def right_rotate_by_k(head: Node, k: int) -> Node: new_head = right_rotate_by_k(head, k) print("After", k, "iterations: ", end="") - print_linked_list(new_head) \ No newline at end of file + print_linked_list(new_head) From 64e0196c29d0cfc0597a9b119e06cf66ad957477 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 17:56:20 +0500 Subject: [PATCH 03/14] updated variable name. --- .../linked_list/rotate_linked_list_by_k.py | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index 809d9b83afab..16c93a7e8f8d 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -7,12 +7,15 @@ def __init__(self, data: int) -> None: self.next = None -def print_linked_list(head: Node) -> None: +def print_linked_list(head: Node | None) -> None: """ Print the entire linked list iteratively. + This function prints the elements of a linked list iteratively, separated by '->'. + Parameters: - head (Node): The head of the linked list to be printed. + head (Node | None): The head of the linked list to be printed,\ +or None if the linked list is empty. >>> head = None >>> head = insert_node(head, 0) @@ -33,12 +36,12 @@ def print_linked_list(head: Node) -> None: print(head.data) -def insert_node(head: Node, data: int) -> Node: +def 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): The head of the linked list. + head (Node | None): The head of the linked list. data (int): The data to be inserted into the new node. Returns: @@ -60,20 +63,21 @@ def insert_node(head: Node, data: int) -> Node: while temp_node.next is not None: temp_node = temp_node.next - temp_node.next = new_node + temp_node.next = new_node # type: ignore return head -def right_rotate_by_k(head: Node, k: int) -> Node: +def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: """ - Rotate a linked list to the right by k times. + Rotate a linked list to the right by rotation times. Parameters: - head (Node): The head of the linked list. - k (int): The number of places to rotate. + head (Node | None): The head of the linked list. + rotation (int): The number of places to rotate. Returns: - Node: The head of the rotated linked list. + Node | None: The head of the rotated linked list\ +if the linked list is not None, otherwise None. >>> head = None >>> head = insert_node(head, 1) @@ -81,8 +85,8 @@ def right_rotate_by_k(head: Node, k: int) -> Node: >>> head = insert_node(head, 3) >>> head = insert_node(head, 4) >>> head = insert_node(head, 5) - >>> k = 2 - >>> new_head = right_rotate_by_k(head, k) + >>> rotation = 2 + >>> new_head = right_rotate_by_k(head, rotation) >>> print_linked_list(new_head) 4->5->1->2->3 """ @@ -97,14 +101,14 @@ def right_rotate_by_k(head: Node, k: int) -> Node: length += 1 temp_node = temp_node.next - # Adjust the value of k to avoid unnecessary rotations. - k = k % length + # Adjust the value of rotation to avoid unnecessary rotations. + rotation = rotation % length - if k == 0: + if rotation == 0: return head # As No rotation needed. # Find the new head position after rotation. - new_head_index = length - k + new_head_index = length - rotation # Traverse to the new head position temp_node = head @@ -127,12 +131,11 @@ def right_rotate_by_k(head: Node, k: int) -> Node: doctest.testmod() head = None - - # head = insert_node(head, 5) - # head = insert_node(head, 1) + head = insert_node(head, 5) + head = insert_node(head, 1) head = insert_node(head, 2) head = insert_node(head, 4) - # head = insert_node(head, 3) + head = insert_node(head, 3) print("Original list: ", end="") print_linked_list(head) From a8058adfefb3134d338610ccf1423880a4b61f56 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq <115654418+Muhammadummerr@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:07:49 +0500 Subject: [PATCH 04/14] Update data_structures/linked_list/rotate_linked_list_by_k.py Co-authored-by: Christian Clauss --- data_structures/linked_list/rotate_linked_list_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index 16c93a7e8f8d..63dcdaf2a258 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -14,7 +14,7 @@ def print_linked_list(head: Node | None) -> None: This function prints the elements of a linked list iteratively, separated by '->'. Parameters: - head (Node | None): The head of the linked list to be printed,\ + head (Node | None): The head of the linked list to be printed, or None if the linked list is empty. >>> head = None From edfd0ecb5376ef47f562401852be563867b52360 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:08:15 +0000 Subject: [PATCH 05/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../linked_list/rotate_linked_list_by_k.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index 63dcdaf2a258..516bbd895905 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -9,24 +9,24 @@ def __init__(self, data: int) -> None: def print_linked_list(head: Node | None) -> None: """ - Print the entire linked list iteratively. - - This function prints the elements of a linked list iteratively, separated by '->'. - - Parameters: - head (Node | None): The head of the linked list to be printed, -or None if the linked list is empty. - - >>> head = None - >>> head = insert_node(head, 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 + Print the entire linked list iteratively. + + This function prints the elements of a linked list iteratively, separated by '->'. + + Parameters: + head (Node | None): The head of the linked list to be printed, + or None if the linked list is empty. + + >>> head = None + >>> head = insert_node(head, 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 """ if head is None: return From 1b9e7da7433073f5358e466909f5ce469091087b Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq <115654418+Muhammadummerr@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:08:37 +0500 Subject: [PATCH 06/14] Update data_structures/linked_list/rotate_linked_list_by_k.py Co-authored-by: Christian Clauss --- data_structures/linked_list/rotate_linked_list_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index 516bbd895905..2c497f3aa095 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -102,7 +102,7 @@ def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: temp_node = temp_node.next # Adjust the value of rotation to avoid unnecessary rotations. - rotation = rotation % length + rotation %= length if rotation == 0: return head # As No rotation needed. From e04ed074bce64b125efd969830ae1a67fc91c940 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 15:10:43 +0200 Subject: [PATCH 07/14] Update data_structures/linked_list/rotate_linked_list_by_k.py --- data_structures/linked_list/rotate_linked_list_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index 2c497f3aa095..b475c69b5f1a 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -11,7 +11,7 @@ def print_linked_list(head: Node | None) -> None: """ Print the entire linked list iteratively. - This function prints the elements of a linked list iteratively, separated by '->'. + This function prints the elements of a linked list separated by '->'. Parameters: head (Node | None): The head of the linked list to be printed, From 589cb8cb5b79525163b49ecfed18329480d22050 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 17:45:07 +0200 Subject: [PATCH 08/14] Make Node a dataclass --- .../linked_list/rotate_linked_list_by_k.py | 87 ++++++++++--------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index b475c69b5f1a..cdd3437b094e 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -1,10 +1,12 @@ from __future__ import annotations +from dataclasses import dataclass + +@dataclass class Node: - def __init__(self, data: int) -> None: - self.data = data - self.next = None + data: int + next_node: Node | None = None def print_linked_list(head: Node | None) -> None: @@ -17,8 +19,7 @@ def print_linked_list(head: Node | None) -> None: head (Node | None): The head of the linked list to be printed, or None if the linked list is empty. - >>> head = None - >>> head = insert_node(head, 0) + >>> head = insert_node(None, 0) >>> head = insert_node(head, 2) >>> head = insert_node(head, 1) >>> print_linked_list(head) @@ -30,9 +31,9 @@ def print_linked_list(head: Node | None) -> None: """ if head is None: return - while head.next is not None: + while head.next_node is not None: print(head.data, end="->") - head = head.next + head = head.next_node print(head.data) @@ -47,8 +48,7 @@ def insert_node(head: Node | None, data: int) -> Node: Returns: Node: The new head of the linked list. - >>> head = None - >>> head = insert_node(head, 10) + >>> head = insert_node(None, 10) >>> head = insert_node(head, 9) >>> head = insert_node(head, 8) >>> print_linked_list(head) @@ -60,68 +60,75 @@ def insert_node(head: Node | None, data: int) -> Node: return new_node temp_node = head - while temp_node.next is not None: - temp_node = temp_node.next + while temp_node.next_node: + temp_node = temp_node.next_node - temp_node.next = new_node # type: ignore + temp_node.next_node = new_node # type: ignore return head -def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: +def rotate_to_the_right(head: Node | None, places: int) -> Node: """ - Rotate a linked list to the right by rotation times. + Rotate a linked list to the right by places times. Parameters: - head (Node | None): The head of the linked list. - rotation (int): The number of places to rotate. + head: The head of the linked list. + places: The number of places to rotate. Returns: - Node | None: The head of the rotated linked list\ -if the linked list is not None, otherwise None. - - >>> head = None - >>> head = insert_node(head, 1) + 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) - >>> rotation = 2 - >>> new_head = right_rotate_by_k(head, rotation) + >>> 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 - if head is None or head.next is None: + if not head: + raise ValueError("The linked list is empty.") + + if head.next_node is None: return head # Calculate the length of the linked list length = 1 temp_node = head - while temp_node.next is not None: + while temp_node.next_node is not None: length += 1 - temp_node = temp_node.next + temp_node = temp_node.next_node - # Adjust the value of rotation to avoid unnecessary rotations. - rotation %= length + # Adjust the value of places to avoid places longer than the list. + places %= length - if rotation == 0: - return head # As No rotation needed. + if places == 0: + return head # As no rotation is needed. # Find the new head position after rotation. - new_head_index = length - rotation + new_head_index = length - places # Traverse to the new head position temp_node = head for _ in range(new_head_index - 1): - temp_node = temp_node.next + temp_node = temp_node.next_node # Update pointers to perform rotation - new_head = temp_node.next - temp_node.next = None + new_head = temp_node.next_node + temp_node.next_node = None temp_node = new_head - while temp_node.next is not None: - temp_node = temp_node.next - temp_node.next = head + while temp_node.next_node: + temp_node = temp_node.next_node + temp_node.next_node = head return new_head @@ -130,8 +137,7 @@ def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: import doctest doctest.testmod() - head = None - head = insert_node(head, 5) + head = insert_node(None, 5) head = insert_node(head, 1) head = insert_node(head, 2) head = insert_node(head, 4) @@ -140,8 +146,7 @@ def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: print("Original list: ", end="") print_linked_list(head) - k = 3 - new_head = right_rotate_by_k(head, k) + new_head = rotate_to_the_right(head, places=3) print("After", k, "iterations: ", end="") print_linked_list(new_head) From c0a1f8ebbdab07c6c8183ae02aa01df064469c93 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 17:48:02 +0200 Subject: [PATCH 09/14] Update rotate_linked_list_by_k.py --- data_structures/linked_list/rotate_linked_list_by_k.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index cdd3437b094e..b8ee1a02ab04 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -146,7 +146,8 @@ def rotate_to_the_right(head: Node | None, places: int) -> Node: print("Original list: ", end="") print_linked_list(head) - new_head = rotate_to_the_right(head, places=3) + places = 3 + new_head = rotate_to_the_right(head, places) - print("After", k, "iterations: ", end="") + print(f"After {places} iterations: ", end="") print_linked_list(new_head) From b575263e460075b41a496e592c17db9fd6463d46 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 17:54:53 +0200 Subject: [PATCH 10/14] Update rotate_linked_list_by_k.py --- data_structures/linked_list/rotate_linked_list_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_linked_list_by_k.py index b8ee1a02ab04..0acd7252c38a 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_linked_list_by_k.py @@ -67,7 +67,7 @@ def insert_node(head: Node | None, data: int) -> Node: return head -def rotate_to_the_right(head: Node | None, places: int) -> Node: +def rotate_to_the_right(head: Node, places: int) -> Node: """ Rotate a linked list to the right by places times. From 0644ed0a9a4f5cd49c51230f378b7ffddd349b56 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 17:58:53 +0200 Subject: [PATCH 11/14] Update and rename rotate_linked_list_by_k.py to rotate_to_the_right.py --- .../{rotate_linked_list_by_k.py => rotate_to_the_right.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename data_structures/linked_list/{rotate_linked_list_by_k.py => rotate_to_the_right.py} (99%) diff --git a/data_structures/linked_list/rotate_linked_list_by_k.py b/data_structures/linked_list/rotate_to_the_right.py similarity index 99% rename from data_structures/linked_list/rotate_linked_list_by_k.py rename to data_structures/linked_list/rotate_to_the_right.py index 0acd7252c38a..e65a651953cd 100644 --- a/data_structures/linked_list/rotate_linked_list_by_k.py +++ b/data_structures/linked_list/rotate_to_the_right.py @@ -130,7 +130,7 @@ def rotate_to_the_right(head: Node, places: int) -> Node: temp_node = temp_node.next_node temp_node.next_node = head - return new_head + areturn new_head if __name__ == "__main__": From 69e4ddb43a448a25a6e8f8d5ac31c12c8cdf1109 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 18:00:24 +0200 Subject: [PATCH 12/14] Update rotate_to_the_right.py --- data_structures/linked_list/rotate_to_the_right.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_to_the_right.py b/data_structures/linked_list/rotate_to_the_right.py index e65a651953cd..161f4021321b 100644 --- a/data_structures/linked_list/rotate_to_the_right.py +++ b/data_structures/linked_list/rotate_to_the_right.py @@ -130,7 +130,8 @@ def rotate_to_the_right(head: Node, places: int) -> Node: temp_node = temp_node.next_node temp_node.next_node = head - areturn new_head + assert new_head + return new_head if __name__ == "__main__": From 69aa9fe2b7983fee16a746ebabfd91c31efdb95a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 18:03:37 +0200 Subject: [PATCH 13/14] Update rotate_to_the_right.py --- data_structures/linked_list/rotate_to_the_right.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/linked_list/rotate_to_the_right.py b/data_structures/linked_list/rotate_to_the_right.py index 161f4021321b..822814849a83 100644 --- a/data_structures/linked_list/rotate_to_the_right.py +++ b/data_structures/linked_list/rotate_to_the_right.py @@ -120,6 +120,7 @@ def rotate_to_the_right(head: Node, places: int) -> Node: # Traverse to the new head position temp_node = head for _ in range(new_head_index - 1): + assert temp_node.next_node temp_node = temp_node.next_node # Update pointers to perform rotation From 5439ceac16407893f0cf37c56385c8e100f14d52 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 18:05:23 +0200 Subject: [PATCH 14/14] Update rotate_to_the_right.py --- data_structures/linked_list/rotate_to_the_right.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/linked_list/rotate_to_the_right.py b/data_structures/linked_list/rotate_to_the_right.py index 822814849a83..51b10481c0ce 100644 --- a/data_structures/linked_list/rotate_to_the_right.py +++ b/data_structures/linked_list/rotate_to_the_right.py @@ -124,6 +124,7 @@ def rotate_to_the_right(head: Node, places: int) -> Node: temp_node = temp_node.next_node # Update pointers to perform rotation + assert temp_node.next_node new_head = temp_node.next_node temp_node.next_node = None temp_node = new_head