Question DSA-LL-0061
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 rev_k(h, k):
cur, n = h, 0
while cur and n < k:
cur = cur.nx; n += 1
if n < k:
return h
prev, cur = rev_k(cur, k), h
for _ in range(k):
cur.nx, prev, cur = prev, cur, cur.nx
return prev
print(show(rev_k(make([, , , , , , , ]), )))
- A.
[3, 2, 1, 6, 5, 4, 8, 7]
- B.
[1, 2, 3, 4, 5, 6, 7, 8]
- C.
Sign up free to see the answer and a step-by-step explanation.