Question DSA-LL-0062
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 reorder(h):
slow = fast = h
while fast and fast.nx:
slow, fast = slow.nx, fast.nx.nx
prev, cur = None, slow.nx
slow.nx = None
while cur:
cur.nx, prev, cur = prev, cur, cur.nx
a, b = h, prev
while b:
a.nx, b.nx, a, b = b, a.nx, a.nx, b.nx
return h
print(show(reorder(make([1, , , , ]))))
- A.
[5, 1, 4, 2, 3]
- B.
[1, 2, 3, 4, 5]
- C.
Sign up free to see the answer and a step-by-step explanation.