ProgrammingVery hardAccenture · Cognizant · Python

What does the following Python 3 code print? class Node: def __init__(self, v, nxt=None): self.v = v; self.nxt = nxt n3 = Node(3) n2 = Node(2, n3) n1 = Node(1, n2) out = [] cur = n1 while cur: out.append(cur.v) cur = cur.nxt print(out)

  1. A. [3, 2, 1]
  2. B. [1, 2, 3]
  3. C. [1, 2]
  4. D. [1, 2, 3, None]

Correct answer: B. [1, 2, 3]

Walking the singly linked list from n1 through nxt pointers visits nodes with values 1, 2, 3 in order.

More Python Programming questions

Exams that ask this topic