Count repeated traversal work

A reporting routine visits a singly linked list containing exactly n nodes. It runs the Python code below, where visit reads a value without changing any nodes or references. For each list length, enter the exact integer number of calls to visit and executions of p = p.next over the entire routine. Count only that explicit advance statement, excluding p = head.


Python
for i in range(n):
    p = head
    for j in range(i):
        p = p.next
    visit(p.value)
List length nCalls to visitExecutions of p = p.next
4
7
10

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Singly Linked Lists Questions: