File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ def insert(self, data):
1414 def append (self , data ):
1515 new_node = Node (data )
1616
17- if self .head is None :
17+ if self .is_empty () :
1818 self .head = new_node
1919 return
2020
@@ -25,5 +25,20 @@ def append(self, data):
2525
2626 current_node .next = new_node
2727
28+ def find (self , data ):
29+
30+ if self .is_empty ():
31+ return None
32+
33+ current_node = self .head
34+
35+ while current_node is not None :
36+ if current_node .data == data :
37+ return current_node
38+
39+ current_node = current_node .next
40+
41+ return None
42+
2843 def is_empty (self ):
2944 return self .head == None
Original file line number Diff line number Diff line change @@ -36,6 +36,22 @@ def test_appending_second_value_should_set_head_next(self):
3636 list .append ("Second" )
3737 self .assertEqual (list .head .next .data , "Second" )
3838
39+ def test_find_on_empty_list_should_return_none (self ):
40+ list = LinkedList ()
41+
42+ self .assertIsNone (list .find ("First" ))
43+
44+ def test_find_should_match_valid_node_in_one_value_list (self ):
45+ list = LinkedList ()
46+ list .append ("First" )
47+ self .assertIsNotNone (list .find ("First" ))
48+
49+ def test_find_should_match_valid_node_in_multi_value_list (self ):
50+ list = LinkedList ()
51+ list .append ("First" )
52+ list .append ("Second" )
53+ self .assertIsNotNone (list .find ("Second" ))
54+
3955 def test_is_empty_should_be_true_if_no_head (self ):
4056 list = LinkedList ()
4157 self .assertTrue (list .is_empty ())
You can’t perform that action at this time.
0 commit comments