From f6862e893afb323680b717843f4fb33e268dd178 Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 15:59:16 +0530 Subject: [PATCH 1/9] rotate linked list to right by k --- .../linked_list/rotate_right_by_k.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 data_structures/linked_list/rotate_right_by_k.py diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py new file mode 100644 index 000000000000..e4c02fdef960 --- /dev/null +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -0,0 +1,92 @@ +from __future__ import annotations +import doctest + +class Node: + def __init__(self, data:int)->None: + self.data = data + self.next = None + +def print_list(head:Node) -> None: + ''' + Prints the entire linked list iteratively + >>> head = None + >>> head = insert_node(head, 1) + >>> head = insert_node(head, 2) + >>> head = insert_node(head, 3) + >>> print_list(head) + 1->2->3 + >>> head = insert_node(head, 4) + >>> head = insert_node(head, 5) + >>> print_list(head) + 1->2->3->4->5 + ''' + while head.next is not None: + print(head.data, end='->') + head = head.next + print(head.data) + +def insert_node(head:Node, data:int) -> Node: + ''' + Returns new head of linked list after inserting new node + >>> head = None + >>> head = insert_node(head, 1) + >>> head = insert_node(head, 2) + >>> head = insert_node(head, 3) + >>> print_list(head) + 1->2->3 + ''' + new_node = Node(data) + if head == None: + head = new_node + return head + 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_places:int) -> Node: + ''' + This function receives head and k as input + parameters and returns head of linked list + after rotation to the right by k places + >>> 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_list(new_head) + 4->5->1->2->3 + ''' + if head is None or head.next is None: + return head + for _ in range(k_places): + temp_node = head + while temp_node.next.next is not None: + temp_node = temp_node.next + end = temp_node.next + temp_node.next = None + end.next = head + head = end + return head + +if __name__ == '__main__': + 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) + + print("Original list: ", end='') + print_list(head) + + k = 2 + new_head = right_rotate_by_k(head, k) + + print("After", k, "iterations: ", end='') + print_list(new_head) \ No newline at end of file From 8d2db65dd3691ae8993713d4e7561d561e6c2d45 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 10:32:29 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../linked_list/rotate_right_by_k.py | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index e4c02fdef960..acf7e4dba44e 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -1,13 +1,15 @@ from __future__ import annotations import doctest + class Node: - def __init__(self, data:int)->None: + def __init__(self, data: int) -> None: self.data = data self.next = None -def print_list(head:Node) -> None: - ''' + +def print_list(head: Node) -> None: + """ Prints the entire linked list iteratively >>> head = None >>> head = insert_node(head, 1) @@ -19,14 +21,15 @@ def print_list(head:Node) -> None: >>> head = insert_node(head, 5) >>> print_list(head) 1->2->3->4->5 - ''' + """ while head.next is not None: - print(head.data, end='->') + print(head.data, end="->") head = head.next print(head.data) -def insert_node(head:Node, data:int) -> Node: - ''' + +def insert_node(head: Node, data: int) -> Node: + """ Returns new head of linked list after inserting new node >>> head = None >>> head = insert_node(head, 1) @@ -34,7 +37,7 @@ def insert_node(head:Node, data:int) -> Node: >>> head = insert_node(head, 3) >>> print_list(head) 1->2->3 - ''' + """ new_node = Node(data) if head == None: head = new_node @@ -45,10 +48,11 @@ def insert_node(head:Node, data:int) -> Node: temp_node.next = new_node return head -def right_rotate_by_k(head:Node, k_places:int) -> Node: - ''' - This function receives head and k as input - parameters and returns head of linked list + +def right_rotate_by_k(head: Node, k_places: int) -> Node: + """ + This function receives head and k as input + parameters and returns head of linked list after rotation to the right by k places >>> head = None >>> head = insert_node(head, 1) @@ -56,11 +60,11 @@ def right_rotate_by_k(head:Node, k_places:int) -> Node: >>> head = insert_node(head, 3) >>> head = insert_node(head, 4) >>> head = insert_node(head, 5) - >>> k = 2 + >>> k = 2 >>> new_head = right_rotate_by_k(head, k) >>> print_list(new_head) 4->5->1->2->3 - ''' + """ if head is None or head.next is None: return head for _ in range(k_places): @@ -73,20 +77,21 @@ def right_rotate_by_k(head:Node, k_places:int) -> Node: head = end return head -if __name__ == '__main__': + +if __name__ == "__main__": 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) - print("Original list: ", end='') + print("Original list: ", end="") print_list(head) k = 2 new_head = right_rotate_by_k(head, k) - - print("After", k, "iterations: ", end='') - print_list(new_head) \ No newline at end of file + + print("After", k, "iterations: ", end="") + print_list(new_head) From 4de0a9bbe26281c891ac27a3933714d30c28092b Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:08:08 +0530 Subject: [PATCH 3/9] changes done --- data_structures/linked_list/rotate_right_by_k.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index acf7e4dba44e..a0aaf800649a 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -39,7 +39,7 @@ def insert_node(head: Node, data: int) -> Node: 1->2->3 """ new_node = Node(data) - if head == None: + if head is None: head = new_node return head temp_node = head @@ -79,6 +79,7 @@ def right_rotate_by_k(head: Node, k_places: int) -> Node: if __name__ == "__main__": + doctest.testmod() head = None head = insert_node(head, 1) From 519ab1f44b6e77f617a32e7ccc7a4b1dd6204008 Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:30:30 +0530 Subject: [PATCH 4/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index a0aaf800649a..8e62c4d7a513 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -1,4 +1,5 @@ from __future__ import annotations + import doctest From 5b7d2608ca27a21496465b41e9ab224063f8e356 Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:34:17 +0530 Subject: [PATCH 5/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index 8e62c4d7a513..1fcc3f2e3026 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -29,7 +29,7 @@ def print_list(head: Node) -> None: print(head.data) -def insert_node(head: Node, data: int) -> Node: +def insert_node(head: Node | None, data: int) -> Node: """ Returns new head of linked list after inserting new node >>> head = None From 01d161aa0628e2f3ec7aa77a84f36ea4d833ecac Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:38:00 +0530 Subject: [PATCH 6/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index 1fcc3f2e3026..d9ca058011e2 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -9,7 +9,7 @@ def __init__(self, data: int) -> None: self.next = None -def print_list(head: Node) -> None: +def print_list(head: Node | None) -> None: """ Prints the entire linked list iteratively >>> head = None @@ -23,10 +23,11 @@ def print_list(head: Node) -> None: >>> print_list(head) 1->2->3->4->5 """ - while head.next is not None: - print(head.data, end="->") - head = head.next - print(head.data) + if head is not None: + while head.next is not None: + print(head.data, end="->") + head = head.next + print(head.data) def insert_node(head: Node | None, data: int) -> Node: @@ -50,7 +51,7 @@ def insert_node(head: Node | None, data: int) -> Node: return head -def right_rotate_by_k(head: Node, k_places: int) -> Node: +def right_rotate_by_k(head: Node | None, k_places: int) -> Node: """ This function receives head and k as input parameters and returns head of linked list From 80543df8575bfd478707632d47e9c1e8677c120c Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:40:19 +0530 Subject: [PATCH 7/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index d9ca058011e2..a426cf0fd743 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -51,7 +51,7 @@ def insert_node(head: Node | None, data: int) -> Node: return head -def right_rotate_by_k(head: Node | None, k_places: int) -> Node: +def right_rotate_by_k(head: Node | None, k_places: int) -> Node | None: """ This function receives head and k as input parameters and returns head of linked list From f740cb605c50332fea06187294a31ec739fd46a9 Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:49:46 +0530 Subject: [PATCH 8/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index a426cf0fd743..a33fdfb1d29c 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -4,7 +4,7 @@ class Node: - def __init__(self, data: int) -> None: + def __init__(self, data: int) -> None | Node: self.data = data self.next = None From 9f2b35e951d6af3a58ece08de728aa94b3b94eec Mon Sep 17 00:00:00 2001 From: parth Date: Sun, 1 Oct 2023 16:53:34 +0530 Subject: [PATCH 9/9] more changes done --- data_structures/linked_list/rotate_right_by_k.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/rotate_right_by_k.py b/data_structures/linked_list/rotate_right_by_k.py index a33fdfb1d29c..5c71e0ea056e 100644 --- a/data_structures/linked_list/rotate_right_by_k.py +++ b/data_structures/linked_list/rotate_right_by_k.py @@ -4,7 +4,7 @@ class Node: - def __init__(self, data: int) -> None | Node: + def __init__(self, data: int) -> None: self.data = data self.next = None @@ -82,9 +82,8 @@ def right_rotate_by_k(head: Node | None, k_places: int) -> Node | None: if __name__ == "__main__": doctest.testmod() - head = None - head = insert_node(head, 1) + head = insert_node(None, 1) head = insert_node(head, 2) head = insert_node(head, 3) head = insert_node(head, 4)