Question 31
MediumHow do you delete a node from a singly linked list when you have a pointer only to that node (not the last one)?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
How do you delete a node from a singly linked list when you have a pointer only to that node (not the last one)?
It is impossible
Traverse from the head
Set the node to null
Copy the next node's data into it, then delete the next node
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of merging two sorted linked lists of lengths m and n?
O(log(m + n))
O(m + n)
O(m × n)
O(1)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, nxt=None):
self.v, self.next = v, nxt
def merge(a, b):
dummy = tail = N(0)
while a and b:
if a.v <= b.v:
tail.next, a = a, a.next
else:
tail.next, b = b, b.next
tail = tail.next
tail.next = a or b
return dummy.next
h = merge(N(1, N(3, N(5))), N(2, N(4, N(6))))
out = []
while h:
out.append(str(h.v)); h = h.next
print(" ".join(out))
6 5 4 3 2 1
2 4 6 1 3 5
Sign up free to see the answer and a step-by-step explanation.
Which data structure is best implemented with a doubly linked list plus a hash map?
A binary search tree
An LRU cache
A min-heap
A trie
Sign up free to see the answer and a step-by-step explanation.
What 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, N(5)))))
prev, cur = None, h
while cur:
cur.nx, prev, cur = prev, cur, cur.nx
out = []
while prev:
out.append(prev.v)
prev = prev.nx
print(out)
[5, 4, 3, 2, 1]
[2, 1, 4, 3, 5]
Sign up free to see the answer and a step-by-step explanation.
What 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, N(5, N(6))))))
slow = fast = h
while fast and fast.nx:
slow, fast = slow.nx, fast.nx.nx
print(slow.v)
6
4
Sign up free to see the answer and a step-by-step explanation.
What 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, N(5)))))
n = 2
dummy = N(0, h)
fast = slow = dummy
for _ in range(n + 1):
fast = fast.nx
while fast:
fast, slow = fast.nx, slow.nx
slow.nx = slow.nx.nx
out, c = [], dummy.nx
while c:
out.append(c.v); c = c.nx
print(out)
[1, 2, 3, 5]
[1, 2, 4, 5]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, nx=None):
self.v, self.nx = v, nx
def merge(a, b):
d = t = N(0)
while a and b:
if a.v <= b.v:
t.nx, a = a, a.nx
else:
t.nx, b = b, b.nx
t = t.nx
t.nx = a or b
return d.nx
r = merge(N(1, N(4, N(6))), N(2, N(3, N(7))))
out = []
while r:
out.append(r.v); r = r.nx
print(out)
[1, 2, 4, 3, 6, 7]
[7, 6, 4, 3, 2, 1]
Sign up free to see the answer and a step-by-step explanation.
What 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)
[1, 2, 3, 4]
[4, 3, 2, 1]
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of inserting at the head of a singly linked list?
O(n²)
O(n)
O(1)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
1 3 5 2 4 6
1 2 3 4 5 6
[5]
[1, 2, 3, 4, 5]
3
5
[2, 3, 4, 5]
[1, 3, 4, 5]
[1, 2, 3, 4, 6, 7]
[1, 4, 6, 2, 3, 7]
[2, 1, 3, 4]
[2, 1, 4, 3]