From cfa71dde2fb0b295adfd0c5ba7a94c151a82e006 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 06:41:02 +0530 Subject: [PATCH 01/11] Adds, append, len, print operations for circular linked list --- .../linked_list/circular_linked_list.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 data_structures/linked_list/circular_linked_list.py diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py new file mode 100644 index 000000000000..d29c7689e8ad --- /dev/null +++ b/data_structures/linked_list/circular_linked_list.py @@ -0,0 +1,59 @@ +class Node: + def __init__(self, data): + self.data = data + self.next_ptr = None + + def set_data(self, value): + self.data = value + + def get_data(self): + return self.data + + def set_next(self, value): + self.next_ptr = value + + def get_next(self): + return self.next_ptr + + def has_next(self): + return self.next_ptr is not None + + +class CircularLinkedList: + def __init__(self): + self.head = None + self.length = 0 + + def __len__(self): + return self.length + + def __str__(self): + current_node = self.head + if not current_node: + return "Empty linked list" + + result = [f""] + current_node = current_node.get_next() + + while current_node != self.head: + result.append(f"") + current_node = current_node.get_next() + + return " => ".join(result) + + def append(self, data): + current_node = self.head + + new_node = Node(data) + new_node.set_next(new_node) + + if current_node is None: + self.head = new_node + else: + while current_node.get_next() != self.head: + current_node = current_node.get_next() + + current_node.set_next(new_node) + new_node.set_next(self.head) + + self.length += 1 From b6ffc182ea641a9e98ede6d83f8dcf42a73b3118 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 06:50:04 +0530 Subject: [PATCH 02/11] Adds, prepend support --- .../linked_list/circular_linked_list.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index d29c7689e8ad..f35ccd16631f 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -57,3 +57,29 @@ def append(self, data): new_node.set_next(self.head) self.length += 1 + + def prepend(self, data): + current_node = self.head + + new_node = Node(data) + new_node.set_next(new_node) + + if current_node is None: + self.head = new_node + else: + while current_node.get_next() != self.head: + current_node = current_node.get_next() + + current_node.set_next(new_node) + new_node.set_next(self.head) + + self.head = new_node + + self.length += 1 + + +if __name__ == "__main__": + temp = CircularLinkedList() + for i in range(10): + temp.prepend(i) + print(temp) From 67b2c641cbb56f868bddb3b7a3f25d6bc3485344 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 06:57:58 +0530 Subject: [PATCH 03/11] Adds, delete from front of the list --- .../linked_list/circular_linked_list.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index f35ccd16631f..95cc79a17d6b 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -77,9 +77,29 @@ def prepend(self, data): self.length += 1 + def delete_front(self): + if self.head is None: + raise IndexError() + + current_node = self.head + + if current_node.get_next() == current_node: + self.head, self.length = None, 0 + else: + while current_node.get_next() != self.head: + current_node = current_node.get_next() + + current_node.set_next(self.head.get_next()) + self.head = self.head.get_next() + + self.length -= 1 + if __name__ == "__main__": temp = CircularLinkedList() - for i in range(10): + for i in range(1): temp.prepend(i) print(temp) + + temp.delete_front() + print(temp) From 296dba0086ae28ad539bf9c28cd7e608a0cde971 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 07:11:08 +0530 Subject: [PATCH 04/11] Adds, delete_rear support --- .../linked_list/circular_linked_list.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 95cc79a17d6b..4804a603342d 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -94,12 +94,29 @@ def delete_front(self): self.length -= 1 + def delete_rear(self): + if self.head is None: + raise IndexError() + + temp_node, current_node = self.head, self.head + + if current_node.get_next() == current_node: + self.head, self.length = None, 0 + else: + while current_node.get_next() != self.head: + temp_node = current_node + current_node = current_node.get_next() + + temp_node.set_next(current_node.get_next()) + + self.length -= 1 + if __name__ == "__main__": temp = CircularLinkedList() - for i in range(1): + for i in range(10): temp.prepend(i) print(temp) - temp.delete_front() + temp.delete_rear() print(temp) From 53d6ba96cc263698499b1367ba317728c2ac5608 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 07:17:13 +0530 Subject: [PATCH 05/11] Adds, method documentations --- .../linked_list/circular_linked_list.py | 59 +++++++++++++++---- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 4804a603342d..2d8cf270d04b 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,33 +1,70 @@ class Node: + """ + Class to represent a single node. + + Each node has following attributes + * data + * next_ptr + """ + def __init__(self, data): self.data = data self.next_ptr = None def set_data(self, value): + """ + Set the data field of the node to given value. + """ self.data = value def get_data(self): + """ + Returns the data field of the current node, + """ return self.data def set_next(self, value): + """ + Sets the next pointer of the current node. + """ self.next_ptr = value def get_next(self): + """ + Returns the next pointer of the current node. + """ return self.next_ptr def has_next(self): + """ + Checks if the current node has a valid next node. + """ return self.next_ptr is not None class CircularLinkedList: + """ + Class to represent the CircularLinkedList. + + CircularLinkedList has following attributes. + * HEAD + * length + """ + def __init__(self): self.head = None self.length = 0 def __len__(self): + """ + Dunder method to return length of the CircularLinkedList + """ return self.length def __str__(self): + """ + Dunder method to represent the string representation of the CircularLinkedList + """ current_node = self.head if not current_node: return "Empty linked list" @@ -42,6 +79,9 @@ def __str__(self): return " => ".join(result) def append(self, data): + """ + Adds a node with given data to the end of the CircularLinkedList + """ current_node = self.head new_node = Node(data) @@ -59,6 +99,9 @@ def append(self, data): self.length += 1 def prepend(self, data): + """ + Adds a ndoe with given data to the front of the CircularLinkedList + """ current_node = self.head new_node = Node(data) @@ -78,6 +121,9 @@ def prepend(self, data): self.length += 1 def delete_front(self): + """ + Removes the 1st node from the CircularLinkedList + """ if self.head is None: raise IndexError() @@ -95,6 +141,9 @@ def delete_front(self): self.length -= 1 def delete_rear(self): + """ + Removes the last node from the CircularLinkedList + """ if self.head is None: raise IndexError() @@ -110,13 +159,3 @@ def delete_rear(self): temp_node.set_next(current_node.get_next()) self.length -= 1 - - -if __name__ == "__main__": - temp = CircularLinkedList() - for i in range(10): - temp.prepend(i) - print(temp) - - temp.delete_rear() - print(temp) From cfd7a9da3fa44622389e961d9bc36e585e2a65c8 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 07:54:05 +0530 Subject: [PATCH 06/11] Adds, type checking and doctests --- .../linked_list/circular_linked_list.py | 87 ++++++++++++++++--- 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 2d8cf270d04b..919cbad21cd8 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,3 +1,6 @@ +import typing + + class Node: """ Class to represent a single node. @@ -7,37 +10,54 @@ class Node: * next_ptr """ - def __init__(self, data): + def __init__(self, data: typing.Any): self.data = data self.next_ptr = None - def set_data(self, value): + def set_data(self, value: typing.Any): """ Set the data field of the node to given value. + >>> node = Node(1) + >>> node.set_data(2) + >>> node.get_data() + 2 """ self.data = value - def get_data(self): + def get_data(self) -> typing.Any: """ - Returns the data field of the current node, + Returns the data field of the current node. + >>> node = Node(1) + >>> node.get_data() + 1 """ return self.data - def set_next(self, value): + def set_next(self, value: typing.Type['Node']): """ Sets the next pointer of the current node. + >>> node, node1 = Node(1), Node(2) + >>> node.set_next(node1) + >>> node.get_next().get_data() + 2 """ self.next_ptr = value - def get_next(self): + def get_next(self) -> typing.Type['Node']: """ Returns the next pointer of the current node. + >>> node = Node(1) + >>> node.get_next() is None + True """ return self.next_ptr - def has_next(self): + def has_next(self) -> bool: """ Checks if the current node has a valid next node. + >>> node = Node(1) + >>> node.has_next() + False """ return self.next_ptr is not None @@ -55,15 +75,28 @@ def __init__(self): self.head = None self.length = 0 - def __len__(self): + def __len__(self) -> int: """ Dunder method to return length of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> len(cll) + 0 + >>> cll.append(1) + >>> len(cll) + 1 """ return self.length - def __str__(self): + def __str__(self) -> str: """ Dunder method to represent the string representation of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> print(cll) + Empty linked list + >>> cll.append(1) + >>> cll.append(2) + >>> print(cll) + => """ current_node = self.head if not current_node: @@ -78,9 +111,16 @@ def __str__(self): return " => ".join(result) - def append(self, data): + def append(self, data: typing.Any): """ Adds a node with given data to the end of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.append(1) + >>> cll.append(2) + >>> len(cll) + 2 + >>> print(cll) + => """ current_node = self.head @@ -98,9 +138,16 @@ def append(self, data): self.length += 1 - def prepend(self, data): + def prepend(self, data: typing.Any): """ Adds a ndoe with given data to the front of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.prepend(1) + >>> cll.prepend(2) + >>> len(cll) + 2 + >>> print(cll) + => """ current_node = self.head @@ -123,6 +170,15 @@ def prepend(self, data): def delete_front(self): """ Removes the 1st node from the CircularLinkedList + >>> cll = CircularLinkedList() + >>> for i in range(5) + ... cll.append(i) + ... + >>> print(cll) + => => => => + >>> cll.delete_front() + >>> print(cll) + => => => """ if self.head is None: raise IndexError() @@ -143,6 +199,15 @@ def delete_front(self): def delete_rear(self): """ Removes the last node from the CircularLinkedList + >>> cll = CircularLinkedList() + >>> for i in range(5) + ... cll.append(i) + ... + >>> print(cll) + => => => => + >>> cll.delete_rear() + >>> print(cll) + => => => """ if self.head is None: raise IndexError() From 906670e9346080e677676f2ad1c0123af52910fa Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 08:00:37 +0530 Subject: [PATCH 07/11] Updates doctest for delete ops --- .../linked_list/circular_linked_list.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 919cbad21cd8..72c3268677b3 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -171,14 +171,13 @@ def delete_front(self): """ Removes the 1st node from the CircularLinkedList >>> cll = CircularLinkedList() - >>> for i in range(5) - ... cll.append(i) - ... + >>> cll.append(1) + >>> cll.append(2) >>> print(cll) - => => => => + => >>> cll.delete_front() >>> print(cll) - => => => + """ if self.head is None: raise IndexError() @@ -200,14 +199,13 @@ def delete_rear(self): """ Removes the last node from the CircularLinkedList >>> cll = CircularLinkedList() - >>> for i in range(5) - ... cll.append(i) - ... + >>> cll.append(1) + >>> cll.append(2) >>> print(cll) - => => => => + => >>> cll.delete_rear() >>> print(cll) - => => => + """ if self.head is None: raise IndexError() From b2090216ed497c27302fdb205cd35396f34ba237 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 13:15:53 +0530 Subject: [PATCH 08/11] Addressing requested changes --- .../linked_list/circular_linked_list.py | 123 ++++++------------ 1 file changed, 42 insertions(+), 81 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 72c3268677b3..88f41d9d6e68 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,4 +1,4 @@ -import typing +from typing import Any, Type class Node: @@ -10,64 +10,17 @@ class Node: * next_ptr """ - def __init__(self, data: typing.Any): + def __init__(self, data: Any): self.data = data self.next_ptr = None - def set_data(self, value: typing.Any): - """ - Set the data field of the node to given value. - >>> node = Node(1) - >>> node.set_data(2) - >>> node.get_data() - 2 - """ - self.data = value - - def get_data(self) -> typing.Any: - """ - Returns the data field of the current node. - >>> node = Node(1) - >>> node.get_data() - 1 - """ - return self.data - - def set_next(self, value: typing.Type['Node']): - """ - Sets the next pointer of the current node. - >>> node, node1 = Node(1), Node(2) - >>> node.set_next(node1) - >>> node.get_next().get_data() - 2 - """ - self.next_ptr = value - - def get_next(self) -> typing.Type['Node']: - """ - Returns the next pointer of the current node. - >>> node = Node(1) - >>> node.get_next() is None - True - """ - return self.next_ptr - - def has_next(self) -> bool: - """ - Checks if the current node has a valid next node. - >>> node = Node(1) - >>> node.has_next() - False - """ - return self.next_ptr is not None - class CircularLinkedList: """ Class to represent the CircularLinkedList. CircularLinkedList has following attributes. - * HEAD + * head * length """ @@ -102,43 +55,43 @@ def __str__(self) -> str: if not current_node: return "Empty linked list" - result = [f""] - current_node = current_node.get_next() + result = [f""] + current_node = current_node.next_ptr while current_node != self.head: - result.append(f"") - current_node = current_node.get_next() + result.append(f"") + current_node = current_node.next_ptr return " => ".join(result) - def append(self, data: typing.Any): + def append(self, data: Any): """ Adds a node with given data to the end of the CircularLinkedList >>> cll = CircularLinkedList() >>> cll.append(1) + >>> print(f"{len(cll)}: {cll}") + 1: >>> cll.append(2) - >>> len(cll) - 2 - >>> print(cll) - => + >>> print(f"{len(cll)}: {cll}") + 2: => """ current_node = self.head new_node = Node(data) - new_node.set_next(new_node) + new_node.next_ptr = new_node if current_node is None: self.head = new_node else: - while current_node.get_next() != self.head: - current_node = current_node.get_next() + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr - current_node.set_next(new_node) - new_node.set_next(self.head) + current_node.next_ptr = new_node + new_node.next_ptr = self.head self.length += 1 - def prepend(self, data: typing.Any): + def prepend(self, data: Any): """ Adds a ndoe with given data to the front of the CircularLinkedList >>> cll = CircularLinkedList() @@ -152,16 +105,16 @@ def prepend(self, data: typing.Any): current_node = self.head new_node = Node(data) - new_node.set_next(new_node) + new_node.next_ptr = new_node if current_node is None: self.head = new_node else: - while current_node.get_next() != self.head: - current_node = current_node.get_next() + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr - current_node.set_next(new_node) - new_node.set_next(self.head) + current_node.next_ptr = new_node + new_node.next_ptr = self.head self.head = new_node @@ -171,6 +124,10 @@ def delete_front(self): """ Removes the 1st node from the CircularLinkedList >>> cll = CircularLinkedList() + >>> cll.delete_rear() + Traceback + ... + IndexError: Deleting from an empty list >>> cll.append(1) >>> cll.append(2) >>> print(cll) @@ -180,18 +137,18 @@ def delete_front(self): """ if self.head is None: - raise IndexError() + raise IndexError("Deleting from an empty list") current_node = self.head - if current_node.get_next() == current_node: + if current_node.next_ptr == current_node: self.head, self.length = None, 0 else: - while current_node.get_next() != self.head: - current_node = current_node.get_next() + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr - current_node.set_next(self.head.get_next()) - self.head = self.head.get_next() + current_node.next_ptr = self.head.next_ptr + self.head = self.head.next_ptr self.length -= 1 @@ -199,6 +156,10 @@ def delete_rear(self): """ Removes the last node from the CircularLinkedList >>> cll = CircularLinkedList() + >>> cll.delete_rear() + Traceback + ... + IndexError: Deleting from an empty list >>> cll.append(1) >>> cll.append(2) >>> print(cll) @@ -208,17 +169,17 @@ def delete_rear(self): """ if self.head is None: - raise IndexError() + raise IndexError("Deleting from an empty list") temp_node, current_node = self.head, self.head - if current_node.get_next() == current_node: + if current_node.next_ptr == current_node: self.head, self.length = None, 0 else: - while current_node.get_next() != self.head: + while current_node.next_ptr != self.head: temp_node = current_node - current_node = current_node.get_next() + current_node = current_node.next_ptr - temp_node.set_next(current_node.get_next()) + temp_node.next_ptr = current_node.next_ptr self.length -= 1 From 90b846322f4c10979f9c54868177b60560de5c27 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 13:19:23 +0530 Subject: [PATCH 09/11] Removes unused import --- data_structures/linked_list/circular_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 88f41d9d6e68..b598175ab5c7 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -1,4 +1,4 @@ -from typing import Any, Type +from typing import Any class Node: From ee2c0e3266adc3df5eb8b65ae60a87ae8d980755 Mon Sep 17 00:00:00 2001 From: onlinejudge95 Date: Tue, 19 Nov 2019 13:25:43 +0530 Subject: [PATCH 10/11] Fixes failing doctests --- data_structures/linked_list/circular_linked_list.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index b598175ab5c7..ae8c4eab7a4c 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -125,7 +125,7 @@ def delete_front(self): Removes the 1st node from the CircularLinkedList >>> cll = CircularLinkedList() >>> cll.delete_rear() - Traceback + Traceback (most recent call last): ... IndexError: Deleting from an empty list >>> cll.append(1) @@ -157,7 +157,7 @@ def delete_rear(self): Removes the last node from the CircularLinkedList >>> cll = CircularLinkedList() >>> cll.delete_rear() - Traceback + Traceback (most recent call last): ... IndexError: Deleting from an empty list >>> cll.append(1) @@ -183,3 +183,9 @@ def delete_rear(self): temp_node.next_ptr = current_node.next_ptr self.length -= 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 33e81522fed4f6e25658ca4eafbcfbe4454101ae Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 19 Nov 2019 10:53:17 +0100 Subject: [PATCH 11/11] Minor modifications... --- .../linked_list/circular_linked_list.py | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index ae8c4eab7a4c..cf523f0a4380 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -55,16 +55,16 @@ def __str__(self) -> str: if not current_node: return "Empty linked list" - result = [f""] + results = [current_node.data] current_node = current_node.next_ptr while current_node != self.head: - result.append(f"") + results.append(current_node.data) current_node = current_node.next_ptr - return " => ".join(result) + return " => ".join(f"" for result in results) - def append(self, data: Any): + def append(self, data: Any) -> None: """ Adds a node with given data to the end of the CircularLinkedList >>> cll = CircularLinkedList() @@ -80,63 +80,58 @@ def append(self, data: Any): new_node = Node(data) new_node.next_ptr = new_node - if current_node is None: - self.head = new_node - else: + if current_node: while current_node.next_ptr != self.head: current_node = current_node.next_ptr current_node.next_ptr = new_node new_node.next_ptr = self.head + else: + self.head = new_node self.length += 1 - def prepend(self, data: Any): + def prepend(self, data: Any) -> None: """ Adds a ndoe with given data to the front of the CircularLinkedList >>> cll = CircularLinkedList() >>> cll.prepend(1) >>> cll.prepend(2) - >>> len(cll) - 2 - >>> print(cll) - => + >>> print(f"{len(cll)}: {cll}") + 2: => """ current_node = self.head new_node = Node(data) new_node.next_ptr = new_node - if current_node is None: - self.head = new_node - else: + if current_node: while current_node.next_ptr != self.head: current_node = current_node.next_ptr current_node.next_ptr = new_node new_node.next_ptr = self.head - self.head = new_node - + self.head = new_node self.length += 1 - def delete_front(self): + def delete_front(self) -> None: """ Removes the 1st node from the CircularLinkedList >>> cll = CircularLinkedList() - >>> cll.delete_rear() + >>> cll.delete_front() Traceback (most recent call last): ... IndexError: Deleting from an empty list >>> cll.append(1) >>> cll.append(2) - >>> print(cll) - => + >>> print(f"{len(cll)}: {cll}") + 2: => >>> cll.delete_front() - >>> print(cll) - + >>> print(f"{len(cll)}: {cll}") + 1: """ - if self.head is None: + if not self.head: raise IndexError("Deleting from an empty list") current_node = self.head @@ -152,7 +147,7 @@ def delete_front(self): self.length -= 1 - def delete_rear(self): + def delete_rear(self) -> None: """ Removes the last node from the CircularLinkedList >>> cll = CircularLinkedList() @@ -162,13 +157,13 @@ def delete_rear(self): IndexError: Deleting from an empty list >>> cll.append(1) >>> cll.append(2) - >>> print(cll) - => + >>> print(f"{len(cll)}: {cll}") + 2: => >>> cll.delete_rear() - >>> print(cll) - + >>> print(f"{len(cll)}: {cll}") + 1: """ - if self.head is None: + if not self.head: raise IndexError("Deleting from an empty list") temp_node, current_node = self.head, self.head