Skip to content

Commit f74371f

Browse files
committed
Fixed size=1 for Node initialization in LinkedList bug.
1 parent 35dbfa7 commit f74371f

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

cp_python/data_structures/linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, data, next_=None):
1010
class LinkedList:
1111
def __init__(self, head: Node = None):
1212
self.head = head
13-
self.size = 0
13+
self.size = 0 if head is None else 1
1414

1515
def prepend(self, data) -> None:
1616
"""Inserts an element at the beginning. Time: O(1)."""

cp_python/tests/data_structures/test_linked_list.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import unittest
2-
from functools import partial
3-
from data_structures.linked_list import LinkedList
2+
from data_structures.linked_list import LinkedList, Node
43

54

65
class TestLinkedListCreation(unittest.TestCase):
@@ -79,3 +78,7 @@ def test_search(self):
7978
def test_search_empty(self):
8079
ll = LinkedList()
8180
self.assertEqual(-1, ll.search(1))
81+
82+
def test_empty_list(self):
83+
ll = LinkedList(Node(data=1))
84+
assert ll.size == 1

0 commit comments

Comments
 (0)