Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A chain of nodes holds 3, 1, 4, 1, 5, built as in the lesson. Write functions that walk the chain to give (1) the sum of the values, and (2) how many times a value appears.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The walking loop: current = head, while current is not None, current = current.next.
Check. 3 + 1 + 4 + 1 + 5 = 14. The value 1 appears twice, and 9 not at all.
Answer: 14 2 0.
Log in to join the discussion.
Loading…