Traversing a list means visiting every node once, in order. It's the first step of almost every linked list operation — printing, searching, counting, finding the tail, or reaching position k. In all three types the idea is the same: a pointer curr hops from node to node along the arrows. What changes is which arrows you follow and when you stop.
Put curr on the head and repeat two things: visit the node, then move with curr = curr.next. When curr becomes null you've passed the last node. You can only ever go forward.
Members only · free account
The rest of this lesson, with 4 more sections, 3 dry-run simulators and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson