There are three important thins about linked list. One is traversing a linked list. Following is a common pattern:
current = self.front
while <some condition holds>:
<Do something with the current node.> # Advance to the next node. current = current.nxt
Another thing is mutating a linked structure. Since code that mutates an existing structure is easy to mess up, we are strongly recommended to draw pictures, which helps us understand it better.
The last thing we need to pay attention to is the problem of going to far. For some methods, once we've found what we are looking for, we have gone too far to make the changes we want. An useful solution to this is to walk two references along the list, with one trailing behind a step.
No comments:
Post a Comment