From 793c56d20337daa4e67c993b11f4a6989c96b6a6 Mon Sep 17 00:00:00 2001 From: Arpit <74809468+arpy8@users.noreply.github.com> Date: Mon, 2 Oct 2023 01:31:10 +0530 Subject: [PATCH 1/3] Create remove_duplicates.py --- .../linked_list/remove_duplicates.py | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 data_structures/linked_list/remove_duplicates.py diff --git a/data_structures/linked_list/remove_duplicates.py b/data_structures/linked_list/remove_duplicates.py new file mode 100644 index 000000000000..591c43953b51 --- /dev/null +++ b/data_structures/linked_list/remove_duplicates.py @@ -0,0 +1,138 @@ +from typing import List, Optional + +class Node: + def __init__(self, data: int): + self.data = data + self.next = None + +def create(A: List[int]) -> Optional[Node]: + """ + Create a linked list from an array. + + Args: + A (List[int]): The input array. + + Returns: + Optional[Node]: The head of the linked list. + + Example: + >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + >>> head = create(A) + >>> display(head) + 1 3 5 7 9 11 15 17 19 + """ + if not A: + return None + + head = Node(A[0]) + last = head + + for data in A[1:]: + last.next = Node(data) + last = last.next + + return head + +def display(head: Optional[Node]) -> None: + """ + Display the elements of a linked list. + + Args: + head (Optional[Node]): The head of the linked list. + + Example: + >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + >>> head = create(A) + >>> display(head) + 1 3 5 7 9 11 11 15 17 19 + """ + while head: + print(head.data, end=' ') + head = head.next + print() + +def count(head: Optional[Node]) -> int: + """ + Count the number of nodes in a linked list. + + Args: + head (Optional[Node]): The head of the linked list. + + Returns: + int: The count of nodes. + + Example: + >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + >>> head = create(A) + >>> count(head) + 10 + """ + count = 0 + while head: + count += 1 + head = head.next + return count + +def delete(head: Optional[Node], index: int) -> int: + """ + Delete a node at a given index from a linked list. + + Args: + head (Optional[Node]): The head of the linked list. + index (int): The index at which the node should be deleted (1-based index). + + Returns: + int: The data of the deleted node, or -1 if the index is out of range. + + Example: + >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + >>> head = create(A) + >>> delete(head, 3) + 5 + """ + if index < 1 or index > count(head): + return -1 + + if index == 1: + data = head.data + head = head.next + return data + else: + prev = head + current = head + for _ in range(index - 1): + prev = current + current = current.next + data = current.data + prev.next = current.next + return data + +def remove_duplicates(head: Optional[Node]) -> None: + """ + Remove duplicate elements from a sorted linked list. + + Args: + head (Optional[Node]): The head of the linked list. + + Example: + >>> A = [1, 3, 3, 5, 5, 7, 7, 9] + >>> head = create(A) + >>> remove_duplicates(head) + >>> display(head) + 1 3 5 7 9 + """ + current = head + while current and current.next: + if current.data == current.next.data: + current.next = current.next.next + else: + current = current.next + +if __name__ == "__main__": + print("Linked List!") + + A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + head = create(A) + + remove_duplicates(head) + display(head) From 456be6a60373b1870ef2963c4a9efd47d69c1276 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 20:04:26 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../linked_list/remove_duplicates.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/remove_duplicates.py b/data_structures/linked_list/remove_duplicates.py index 591c43953b51..2874b84773cc 100644 --- a/data_structures/linked_list/remove_duplicates.py +++ b/data_structures/linked_list/remove_duplicates.py @@ -1,10 +1,12 @@ from typing import List, Optional + class Node: def __init__(self, data: int): self.data = data self.next = None + def create(A: List[int]) -> Optional[Node]: """ Create a linked list from an array. @@ -23,16 +25,17 @@ def create(A: List[int]) -> Optional[Node]: """ if not A: return None - + head = Node(A[0]) last = head - + for data in A[1:]: last.next = Node(data) last = last.next - + return head + def display(head: Optional[Node]) -> None: """ Display the elements of a linked list. @@ -47,10 +50,11 @@ def display(head: Optional[Node]) -> None: 1 3 5 7 9 11 11 15 17 19 """ while head: - print(head.data, end=' ') + print(head.data, end=" ") head = head.next print() + def count(head: Optional[Node]) -> int: """ Count the number of nodes in a linked list. @@ -73,6 +77,7 @@ def count(head: Optional[Node]) -> int: head = head.next return count + def delete(head: Optional[Node], index: int) -> int: """ Delete a node at a given index from a linked list. @@ -107,6 +112,7 @@ def delete(head: Optional[Node], index: int) -> int: prev.next = current.next return data + def remove_duplicates(head: Optional[Node]) -> None: """ Remove duplicate elements from a sorted linked list. @@ -128,11 +134,12 @@ def remove_duplicates(head: Optional[Node]) -> None: else: current = current.next + if __name__ == "__main__": print("Linked List!") A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] head = create(A) - + remove_duplicates(head) display(head) From 15e0edcbdd8265b3ef89555a420e540fc68c6fab Mon Sep 17 00:00:00 2001 From: Arpit <74809468+arpy8@users.noreply.github.com> Date: Mon, 2 Oct 2023 03:16:46 +0530 Subject: [PATCH 3/3] Update remove_duplicates.py --- .../linked_list/remove_duplicates.py | 142 ++++++++++-------- 1 file changed, 78 insertions(+), 64 deletions(-) diff --git a/data_structures/linked_list/remove_duplicates.py b/data_structures/linked_list/remove_duplicates.py index 2874b84773cc..f6051d8ef6e2 100644 --- a/data_structures/linked_list/remove_duplicates.py +++ b/data_structures/linked_list/remove_duplicates.py @@ -1,131 +1,147 @@ -from typing import List, Optional - - class Node: - def __init__(self, data: int): + def __init__(self, data: int = None): self.data = data self.next = None + def __repr__(self): + """Returns a visual representation of the node and all its following nodes.""" + string_rep = "" + temp = self + while temp: + string_rep += f"<{temp.data}> ---> " + temp = temp.next + string_rep += "" + return string_rep -def create(A: List[int]) -> Optional[Node]: + +def create(arr: list[int]) -> Node: """ - Create a linked list from an array. + Creates a linked list from an array and returns the head of the linked list. Args: - A (List[int]): The input array. + arr (list[int]): The array of integers. Returns: - Optional[Node]: The head of the linked list. + Node: The head of the linked list. Example: - >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] - >>> head = create(A) - >>> display(head) - 1 3 5 7 9 11 15 17 19 + >>> arr = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + >>> head = create(arr) + >>> head.data + 1 + >>> head.next.data + 3 """ - if not A: - return None + if not arr: + raise ValueError("The array is empty") - head = Node(A[0]) - last = head + head = Node(arr[0]) + current = head - for data in A[1:]: - last.next = Node(data) - last = last.next + for data in arr[1:]: + current.next = Node(data) + current = current.next return head -def display(head: Optional[Node]) -> None: +def display(head: Node) -> str: """ - Display the elements of a linked list. + Display the elements of the linked list. Args: - head (Optional[Node]): The head of the linked list. + head (Node): The head of the linked list. + + Returns: + str: A visual representation of the linked list. Example: - >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] - >>> head = create(A) + >>> arr = [1, 3, 5, 7, 9] + >>> head = create(arr) >>> display(head) - 1 3 5 7 9 11 11 15 17 19 + '<1> ---> <3> ---> <5> ---> <7> ---> <9> ---> ' """ - while head: - print(head.data, end=" ") - head = head.next - print() + result = "" + current = head + while current: + result += f"<{current.data}> ---> " + current = current.next + result += "" + return result -def count(head: Optional[Node]) -> int: +def count(head: Node) -> int: """ - Count the number of nodes in a linked list. + Count the number of nodes in the linked list. Args: - head (Optional[Node]): The head of the linked list. + head (Node): The head of the linked list. Returns: - int: The count of nodes. + int: The count of nodes in the linked list. Example: - >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] - >>> head = create(A) + >>> arr = [1, 3, 5, 7, 9] + >>> head = create(arr) >>> count(head) - 10 + 5 """ count = 0 - while head: + current = head + while current: count += 1 - head = head.next + current = current.next return count -def delete(head: Optional[Node], index: int) -> int: +def delete(head: Node, index: int) -> int: """ - Delete a node at a given index from a linked list. + Delete a node at a given index from the linked list and return its value. Args: - head (Optional[Node]): The head of the linked list. + head (Node): The head of the linked list. index (int): The index at which the node should be deleted (1-based index). Returns: - int: The data of the deleted node, or -1 if the index is out of range. + int: The value of the deleted node. Example: - >>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] - >>> head = create(A) + >>> arr = [1, 3, 5, 7, 9] + >>> head = create(arr) >>> delete(head, 3) 5 + >>> display(head) + '<1> ---> <3> ---> <7> ---> <9> ---> ' """ if index < 1 or index > count(head): return -1 if index == 1: - data = head.data + value = head.data head = head.next - return data + return value else: - prev = head current = head - for _ in range(index - 1): - prev = current + for _ in range(index - 2): current = current.next - data = current.data - prev.next = current.next - return data + value = current.next.data + current.next = current.next.next + return value -def remove_duplicates(head: Optional[Node]) -> None: +def remove_duplicates(head: Node): """ - Remove duplicate elements from a sorted linked list. + Remove duplicates from a sorted linked list. Args: - head (Optional[Node]): The head of the linked list. + head (Node): The head of the sorted linked list. Example: - >>> A = [1, 3, 3, 5, 5, 7, 7, 9] - >>> head = create(A) + >>> arr = [1, 3, 3, 5, 5, 7] + >>> head = create(arr) >>> remove_duplicates(head) >>> display(head) - 1 3 5 7 9 + '<1> ---> <3> ---> <5> ---> <7> ---> ' """ current = head while current and current.next: @@ -136,10 +152,8 @@ def remove_duplicates(head: Optional[Node]) -> None: if __name__ == "__main__": - print("Linked List!") - - A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] - head = create(A) - + arr = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] + head = create(arr) remove_duplicates(head) - display(head) + print("Linked List:") + print(display(head))