Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14478

a question about python linked list deletion method

$
0
0

I follow up a data structures book, and there, we implement a LinkedList class with it's methods, I have a question about the deletion method

    def deletion(self, index):        current_node = self.first_node        current_index = 0        if index == 0:            self.first_node = self.first_node.next_node        else:            while current_index < (index - 1):                current_node = current_node.next_node                current_index += 1            current_node.next_node = current_node.next_node.next_node

and specifically about this line current_node.next_node = current_node.next_node.next_node. Maybe I can't count already, but as far as I understand, if I pass in the index 3, at the end of the while loop the current_node should be at the index 2, but in the book, to delete the index 3 we call current_node.next_node which brings us to the 3rd index and then connects it to the 4th which shouldn't do anything we need, but it actually works. So, do I like, count wrong or something or what?


Viewing all articles
Browse latest Browse all 14478

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>