Question DSA-LL-0045
MediumWhat is the output of this code?
class N:
def __init__(self, v, nx=None):
self.v, self.nx = v, nx
h = N(1, N(2, N(3, N(4))))
def swap_pairs(h):
if not h or not h.nx:
return h
s = h.nx
h.nx = swap_pairs(s.nx)
s.nx = h
return s
r, out = swap_pairs(h), []
while r:
out.append(r.v); r = r.nx
print(out)
- A.
[1, 2, 3, 4]
- B.
[4, 3, 2, 1]
- C.
[2, 1, 3, 4]
- D.
[2, 1, 4, 3]
Sign up free to see the answer and a step-by-step explanation.