A. It does not check if dq.front.next is nil before setting dq.front.prev.
B. It should not set dq.front.prev to nil.
C. It should update dq.rear.prev instead of dq.front.prev.
D. It should set dq.rear to nil when the last element is deleted.
Solution
Step 1: Analyze deletion when only one element exists
If only one node exists, after dq.front = dq.front.next, dq.front becomes nil.
Step 2: Check rear pointer update
Rear pointer still points to old node; it must be set to nil to avoid dangling pointer.
Final Answer:
It should set dq.rear to nil when the last element is deleted. -> Option D
Quick Check:
Update rear on last element deletion [OK]
Hint: Update rear pointer when dequeue becomes empty [OK]
Common Mistakes:
Not updating rear when front becomes nil
Assuming front.prev always exists
Confusing prev and next pointers
5. You want to implement a function in Go that reverses the elements of a dequeue implemented using a doubly linked list. Which approach correctly reverses the dequeue in-place?
hard
A. Traverse each node, swap its next and prev pointers, then swap front and rear pointers of the dequeue.
B. Create a new dequeue and insert elements from the old dequeue's front to rear into the new dequeue's rear.
C. Swap only the data values of front and rear nodes repeatedly until the middle is reached.
D. Delete all elements from front and insert them at rear repeatedly until original dequeue is empty.
Solution
Step 1: Understand in-place reversal of doubly linked list
Swapping next and prev pointers of each node reverses the links.
Step 2: Update dequeue front and rear pointers
After swapping pointers, front and rear pointers must be swapped to reflect new ends.
Final Answer:
Traverse each node, swap its next and prev pointers, then swap front and rear pointers of the dequeue. -> Option A
Quick Check:
Swap pointers in nodes + swap front/rear [OK]
Hint: Swap next/prev in nodes, then swap front and rear [OK]
Common Mistakes:
Creating new dequeue instead of in-place reversal
Swapping only data values without pointers
Deleting and reinserting nodes unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.