Trace consecutive node removals

A singly linked list initially stores 8, 3, 4, 9, 2 and 7 in that order. Each node has value and next fields. Run the following Python code once. Determine the final value of removed, in nodes, and the sum of the values still reachable from head. Give both answers as exact integers.


Six linked nodes store the values 8, 3, 4, 9, 2 and 7 in that order.


Python
prev = None
cur = head
removed = 0

while cur is not None:
    if cur.value < 5:
        if prev is None:
            head = cur.next
        else:
            prev.next = cur.next
        removed += 1
    else:
        prev = cur
    cur = cur.next
Your answers

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: