It will be much easier to understand the codes if we can draw pictures for LinkedList.
We will find the codes below quite useful for tracing the values of the LinkedList.
cur_node = self.front
while <some condition here...>:
# do something here...
cur_node = cur_node.nxt
If the mutation does not only relate to the current nodes but also relate to the previous nodes. (For example, delete_back) We will find the codes below quite useful.
prev_node, cur_node = None, lnk.front# walk along until cur_node is lnk.back while <some condition>:prev_node = cur_nodecur_node = cur_node.nxtWhen we mutate the LinkedList, we will probably change the variables like self.front, self.back, self.size, self.value and self.nxt. What can't be ignored is that it's confusing when we change some variables and the best way to make it less complicated is drawing clear pictures carefully.When it comes to adding a brand new node, we need to create a new node by writing LLNode(value,nxt) and link it with the previous node and the next node.

没有评论:
发表评论