Question DSA-LL-0064
HardWhat is the output of this code?
class N:
def __init__(self, v, nx=None):
self.v, self.nx = v, nx
def make(xs):
h = None
for x in reversed(xs):
h = N(x, h)
return h
def show(h):
out = []
while h:
out.append(h.v); h = h.nx
return out
def detect(h):
slow = fast = h
while fast and fast.nx:
slow, fast = slow.nx, fast.nx.nx
if slow is fast:
p = h
while p is not slow:
p, slow = p.nx, slow.nx
return p.v
return None
h = make([, , , , , ])
t = h
t.nx:
t = t.nx
t.nx = h.nx.nx
(detect(h))
- A.
6
- B.
3
- C.
Sign up free to see the answer and a step-by-step explanation.