donnemartin / interactive-coding-challenges

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Other
29.44k stars 4.45k forks source link

Append method of linkedlist implementation has a bug #173

Closed glang closed 7 years ago

glang commented 7 years ago

https://github.com/donnemartin/interactive-coding-challenges/blob/master/linked_lists/linked_list/linked_list_solution.ipynb

When self.head == None, the node returned is not the node assigned to self.head. self.head should be assigned node.

I just found that the delete method also has a bug. When self.head.data == data, self.head is set to None and that deletes the whole linkedlist and not just the head... self.head should be set to self.head.next in that case.

donnemartin commented 7 years ago

Hi @glang good catch! Are you interested in submitting a pull request? Thanks!

eamanu commented 7 years ago

Hi @glang!

I am trying to follow the problem. Where the self.head should be return an assigned node? Is in def append(self, data)?

Regards!

glang commented 7 years ago

@donnemartin I see @eamanu has already fixed the delete method bug, as for the append method bug,

        if self.head is None:
            self.head = Node(data) #should be self.head = node
            return node