From dd60854f455d68d5a4781d007d328afab6ff6afd Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 25 Sep 2020 11:20:32 +0800 Subject: [PATCH 01/15] Updated singly_linked_list --- .../linked_list/singly_linked_list.py | 165 +++++++++++------- 1 file changed, 103 insertions(+), 62 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 1f3e03a31b7e..e57962c0f386 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -4,52 +4,60 @@ def __init__(self, data): self.next = None def __repr__(self): - return f"Node({self.data})" + return f"{self.data}" class LinkedList: def __init__(self): self.head = None # initialize head to None + self.size = 0 # length of linked list def insert_tail(self, data) -> None: - if self.head is None: - self.insert_head(data) # if this is first node, call insert_head - else: - temp = self.head - while temp.next: # traverse to last node - temp = temp.next - temp.next = Node(data) # create node & link to tail + self.insert_nth(self.size, data) def insert_head(self, data) -> None: + self.insert_nth(0, data) + + def insert_nth(self, index: int, data) -> None: + if index < 0 or index > self.size: # test if index is valid. + raise IndexError("list index out of range.") new_node = Node(data) # create a new node - if self.head: + if self.head is None: + self.head = new_node + elif index == 0: new_node.next = self.head # link new_node to head - self.head = new_node # make NewNode as head + self.head = new_node # make NewNode as head + else: + temp = self.head + for _ in range(index - 1): + temp = temp.next + new_node.next = temp.next + temp.next = new_node + self.size += 1 def print_list(self) -> None: # print every node data - temp = self.head - while temp: - print(temp.data) - temp = temp.next + print(self) def delete_head(self): # delete from head - temp = self.head - if self.head: - self.head = self.head.next - temp.next = None - return temp + return self.delete_nth(0) def delete_tail(self): # delete from tail - temp = self.head - if self.head: - if self.head.next is None: # if head is the only Node in the Linked List - self.head = None - else: - while temp.next.next: # find the 2nd last element - temp = temp.next - # (2nd last element).next = None and temp = last element - temp.next, temp = None, temp.next - return temp + return self.delete_nth(self.size - 1) + + def delete_nth(self, index: int): + if index < 0 or index > self.size - 1: # test if index is valid + raise IndexError("list index out of range.") + delete_node = self.head # default first node + if index == 0: + self.head = self.head.next + else: + temp = self.head + for _ in range(index - 1): + temp = temp.next + delete_node = temp.next + temp.next = temp.next.next + self.size -= 1 + return delete_node.data def is_empty(self) -> bool: return self.head is None # return True if head is none @@ -72,12 +80,14 @@ def reverse(self): def __repr__(self): # String representation/visualization of a Linked Lists current = self.head - string_repr = "" + string_repr = [] while current: - string_repr += f"{current} --> " + string_repr.append(f"{current.data}") current = current.next - # END represents end of the LinkedList - return string_repr + "END" + return "->".join(string_repr) + + def __str__(self) -> str: + return repr(self) # Indexing Support. Used to get a node at particular position def __getitem__(self, index): @@ -93,21 +103,21 @@ def __getitem__(self, index): if current.next is None: raise IndexError("Index out of range.") current = current.next - return current + return current.data # Used to change the data of a particular node def __setitem__(self, index, data): current = self.head # If list is empty if current is None: - raise IndexError("The Linked List is empty") + raise IndexError("The Linked List is empty.") for i in range(index): if current.next is None: raise IndexError("Index out of range.") current = current.next current.data = data - def __len__(self): + def __len__(self) -> int: """ Return length of linked list i.e. number of nodes >>> linked_list = LinkedList() @@ -126,45 +136,76 @@ def __len__(self): >>> len(linked_list) 0 """ - if not self.head: - return 0 - - count = 0 - cur_node = self.head - while cur_node.next: - count += 1 - cur_node = cur_node.next - return count + 1 + return self.size + + +def test_singly_linked_list() -> None: + """ + >>> test_singly_linked_list() + """ + linked_list = LinkedList() + assert linked_list.is_empty() is True + assert str(linked_list) == "" + + try: + linked_list.delete_head() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + try: + linked_list.delete_tail() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + for i in range(10): + assert len(linked_list) == i + linked_list.insert_nth(i, i + 1) + assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) + + linked_list.insert_head(0) + linked_list.insert_tail(11) + assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) + + assert linked_list.delete_head() == 0 + assert linked_list.delete_nth(9) == 10 + assert linked_list.delete_tail() == 11 + assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) def main(): - A = LinkedList() - A.insert_head(input("Inserting 1st at head ").strip()) - A.insert_head(input("Inserting 2nd at head ").strip()) + from doctest import testmod + testmod() + test_singly_linked_list() + + linked_list = LinkedList() + linked_list.insert_head(input("Inserting 1st at head ").strip()) + linked_list.insert_head(input("Inserting 2nd at head ").strip()) print("\nPrint list:") - A.print_list() - A.insert_tail(input("\nInserting 1st at tail ").strip()) - A.insert_tail(input("Inserting 2nd at tail ").strip()) + linked_list.print_list() + linked_list.insert_tail(input("\nInserting 1st at tail ").strip()) + linked_list.insert_tail(input("Inserting 2nd at tail ").strip()) print("\nPrint list:") - A.print_list() + linked_list.print_list() print("\nDelete head") - A.delete_head() + linked_list.delete_head() print("Delete tail") - A.delete_tail() + linked_list.delete_tail() print("\nPrint list:") - A.print_list() + linked_list.print_list() print("\nReverse linked list") - A.reverse() + linked_list.reverse() print("\nPrint list:") - A.print_list() + linked_list.print_list() print("\nString representation of linked list:") - print(A) + print(linked_list) print("\nReading/changing Node data using indexing:") - print(f"Element at Position 1: {A[1]}") - A[1] = input("Enter New Value: ").strip() + print(f"Element at Position 1: {linked_list[1]}") + linked_list[1] = input("Enter New Value: ").strip() print("New list:") - print(A) - print(f"length of A is : {len(A)}") + print(linked_list) + print(f"length of linked_list is : {len(linked_list)}") if __name__ == "__main__": From e458450be51cf209524c88ee35e3b97f930c8539 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 25 Sep 2020 03:23:11 +0000 Subject: [PATCH 02/15] fixup! Format Python code with psf/black push --- data_structures/linked_list/singly_linked_list.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index e57962c0f386..b2e9adac048f 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -163,7 +163,7 @@ def test_singly_linked_list() -> None: assert len(linked_list) == i linked_list.insert_nth(i, i + 1) assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) - + linked_list.insert_head(0) linked_list.insert_tail(11) assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) @@ -176,6 +176,7 @@ def test_singly_linked_list() -> None: def main(): from doctest import testmod + testmod() test_singly_linked_list() From 1dbfc3b25c8459a52d9ef146b5e3b6e5fd71202f Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 14:23:58 +0800 Subject: [PATCH 03/15] undo __repr__ --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index b2e9adac048f..c94c60ae7301 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -4,7 +4,7 @@ def __init__(self, data): self.next = None def __repr__(self): - return f"{self.data}" + return f"Node({self.data})" class LinkedList: From 61b4bb7a9cb4a00dd11779e85a59cd23e2e3a5e8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 25 Sep 2020 06:24:56 +0000 Subject: [PATCH 04/15] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 03044e01084b..f08c31794b1e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -25,7 +25,9 @@ * [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py) ## Bit Manipulation + * [Binary And Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_and_operator.py) * [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py) + * [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py) ## Blockchain * [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py) From 91d6f5caee1f54e0be80c57c16c0d49d67ab89a4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 25 Sep 2020 12:22:33 +0200 Subject: [PATCH 05/15] UNTESTED CHANGES: Add an .__iter__() method. This will break tests, etc. --- .../linked_list/singly_linked_list.py | 125 +++++++++--------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index c94c60ae7301..24190f521f19 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -10,7 +10,64 @@ def __repr__(self): class LinkedList: def __init__(self): self.head = None # initialize head to None - self.size = 0 # length of linked list + + def __iter__(self): + node = self.head + while node: + yield node.data + node = node.next + + def __len__(self) -> int: + """ + Return length of linked list i.e. number of nodes + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.insert_tail("head") + >>> len(linked_list) + 1 + >>> linked_list.insert_head("head") + >>> len(linked_list) + 2 + >>> _ = linked_list.delete_tail() + >>> len(linked_list) + 1 + >>> _ = linked_list.delete_head() + >>> len(linked_list) + 0 + """ + return len(tuple(self)) + + def __repr__(self): + """ + String representation/visualization of a Linked Lists + """ + return str(tuple(self)) + + def __str__(self) -> str: + return repr(self) + + def __getitem__(self, index): + """ + Indexing Support. Used to get a node at particular position + """ + if index < 0: + raise ValueError("Negative indexes are not yet supported") + for i, node in enumerate(self): + if i == index: + return node.data + + # Used to change the data of a particular node + def __setitem__(self, index, data): + current = self.head + # If list is empty + if current is None: + raise IndexError("The Linked List is empty") + for i in range(index): + if current.next is None: + raise IndexError("list index out of range") + current = current.next + current.data = data def insert_tail(self, data) -> None: self.insert_nth(self.size, data) @@ -19,8 +76,8 @@ def insert_head(self, data) -> None: self.insert_nth(0, data) def insert_nth(self, index: int, data) -> None: - if index < 0 or index > self.size: # test if index is valid. - raise IndexError("list index out of range.") + if not 0 <= index < len(self): + raise IndexError("list index out of range") new_node = Node(data) # create a new node if self.head is None: self.head = new_node @@ -46,7 +103,7 @@ def delete_tail(self): # delete from tail def delete_nth(self, index: int): if index < 0 or index > self.size - 1: # test if index is valid - raise IndexError("list index out of range.") + raise IndexError("list index out of range") delete_node = self.head # default first node if index == 0: self.head = self.head.next @@ -78,66 +135,6 @@ def reverse(self): # Return prev in order to put the head at the end self.head = prev - def __repr__(self): # String representation/visualization of a Linked Lists - current = self.head - string_repr = [] - while current: - string_repr.append(f"{current.data}") - current = current.next - return "->".join(string_repr) - - def __str__(self) -> str: - return repr(self) - - # Indexing Support. Used to get a node at particular position - def __getitem__(self, index): - current = self.head - - # If LinkedList is empty - if current is None: - raise IndexError("The Linked List is empty") - - # Move Forward 'index' times - for _ in range(index): - # If the LinkedList ends before reaching specified node - if current.next is None: - raise IndexError("Index out of range.") - current = current.next - return current.data - - # Used to change the data of a particular node - def __setitem__(self, index, data): - current = self.head - # If list is empty - if current is None: - raise IndexError("The Linked List is empty.") - for i in range(index): - if current.next is None: - raise IndexError("Index out of range.") - current = current.next - current.data = data - - def __len__(self) -> int: - """ - Return length of linked list i.e. number of nodes - >>> linked_list = LinkedList() - >>> len(linked_list) - 0 - >>> linked_list.insert_tail("head") - >>> len(linked_list) - 1 - >>> linked_list.insert_head("head") - >>> len(linked_list) - 2 - >>> _ = linked_list.delete_tail() - >>> len(linked_list) - 1 - >>> _ = linked_list.delete_head() - >>> len(linked_list) - 0 - """ - return self.size - def test_singly_linked_list() -> None: """ From 2f9f2b3005ff892d51ffd4166982be8182a3eead Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 25 Sep 2020 10:23:28 +0000 Subject: [PATCH 06/15] fixup! Format Python code with psf/black push --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 24190f521f19..b3ea00437e58 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -46,7 +46,7 @@ def __repr__(self): def __str__(self) -> str: return repr(self) - + def __getitem__(self, index): """ Indexing Support. Used to get a node at particular position From 7a243b692267501b7ab711dd0a44e311fda42413 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 25 Sep 2020 12:41:48 +0200 Subject: [PATCH 07/15] len(tuple(iter(self))) --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index b3ea00437e58..61acdb45bdee 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -36,7 +36,7 @@ def __len__(self) -> int: >>> len(linked_list) 0 """ - return len(tuple(self)) + return len(tuple(iter(self))) def __repr__(self): """ From 7cc7906738d039c32d05f86c142be65489ad5565 Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 25 Sep 2020 20:08:26 +0800 Subject: [PATCH 08/15] fixed __repr__() --- data_structures/linked_list/singly_linked_list.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 61acdb45bdee..a00229e67f61 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -42,7 +42,7 @@ def __repr__(self): """ String representation/visualization of a Linked Lists """ - return str(tuple(self)) + return "->".join([str(item) for item in self]) def __str__(self) -> str: return repr(self) @@ -70,13 +70,13 @@ def __setitem__(self, index, data): current.data = data def insert_tail(self, data) -> None: - self.insert_nth(self.size, data) + self.insert_nth(len(self), data) def insert_head(self, data) -> None: self.insert_nth(0, data) def insert_nth(self, index: int, data) -> None: - if not 0 <= index < len(self): + if not 0 <= index <= len(self): raise IndexError("list index out of range") new_node = Node(data) # create a new node if self.head is None: @@ -90,7 +90,6 @@ def insert_nth(self, index: int, data) -> None: temp = temp.next new_node.next = temp.next temp.next = new_node - self.size += 1 def print_list(self) -> None: # print every node data print(self) @@ -99,10 +98,10 @@ def delete_head(self): # delete from head return self.delete_nth(0) def delete_tail(self): # delete from tail - return self.delete_nth(self.size - 1) + return self.delete_nth(len(self) - 1) def delete_nth(self, index: int): - if index < 0 or index > self.size - 1: # test if index is valid + if not 0 <= index <= len(self) - 1: # test if index is valid raise IndexError("list index out of range") delete_node = self.head # default first node if index == 0: @@ -113,7 +112,6 @@ def delete_nth(self, index: int): temp = temp.next delete_node = temp.next temp.next = temp.next.next - self.size -= 1 return delete_node.data def is_empty(self) -> bool: @@ -175,7 +173,6 @@ def main(): from doctest import testmod testmod() - test_singly_linked_list() linked_list = LinkedList() linked_list.insert_head(input("Inserting 1st at head ").strip()) From 14fd3a7634fa189ce6d1d14d1683986b494efaaa Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:09:19 +0800 Subject: [PATCH 09/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index a00229e67f61..135aaf49fff9 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -9,7 +9,7 @@ def __repr__(self): class LinkedList: def __init__(self): - self.head = None # initialize head to None + self.head = None def __iter__(self): node = self.head From d33b72f569113adf4ddeb615d80b371be6ad11d3 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:16:24 +0800 Subject: [PATCH 10/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 135aaf49fff9..b6bdc8dcbc37 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -44,9 +44,6 @@ def __repr__(self): """ return "->".join([str(item) for item in self]) - def __str__(self) -> str: - return repr(self) - def __getitem__(self, index): """ Indexing Support. Used to get a node at particular position From b95f5a8771296ff3949de9bb17a79dc004015e44 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:17:56 +0800 Subject: [PATCH 11/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index b6bdc8dcbc37..53f6bc5603cc 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -97,7 +97,7 @@ def delete_head(self): # delete from head def delete_tail(self): # delete from tail return self.delete_nth(len(self) - 1) - def delete_nth(self, index: int): + def delete_nth(self, index: int = 0): if not 0 <= index <= len(self) - 1: # test if index is valid raise IndexError("list index out of range") delete_node = self.head # default first node From d9a95d8fcde442d328318a62981cb550c9d355cb Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:19:40 +0800 Subject: [PATCH 12/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 53f6bc5603cc..e9ae3c3ed576 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -112,7 +112,7 @@ def delete_nth(self, index: int = 0): return delete_node.data def is_empty(self) -> bool: - return self.head is None # return True if head is none + return self.head is None def reverse(self): prev = None From 8d2296e8de81f84825c7f47d10e5d26254d98c10 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:20:54 +0800 Subject: [PATCH 13/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index e9ae3c3ed576..dca47009ae14 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -91,7 +91,7 @@ def insert_nth(self, index: int, data) -> None: def print_list(self) -> None: # print every node data print(self) - def delete_head(self): # delete from head + def delete_head(self): return self.delete_nth(0) def delete_tail(self): # delete from tail From ba01f6cceae08455ed16f2afba4bfe1c1dc046d9 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:21:36 +0800 Subject: [PATCH 14/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index dca47009ae14..63330adadebc 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -75,7 +75,7 @@ def insert_head(self, data) -> None: def insert_nth(self, index: int, data) -> None: if not 0 <= index <= len(self): raise IndexError("list index out of range") - new_node = Node(data) # create a new node + new_node = Node(data) if self.head is None: self.head = new_node elif index == 0: From 142ed157ad5bc9f9906e0323285de8208b9ca5ec Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 25 Sep 2020 21:22:08 +0800 Subject: [PATCH 15/15] Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss --- data_structures/linked_list/singly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 63330adadebc..39b14c520521 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -80,7 +80,7 @@ def insert_nth(self, index: int, data) -> None: self.head = new_node elif index == 0: new_node.next = self.head # link new_node to head - self.head = new_node # make NewNode as head + self.head = new_node else: temp = self.head for _ in range(index - 1):