Recall & Review
beginner
What is a doubly linked list?
A doubly linked list is a chain of nodes where each node has three parts: data, a link to the next node, and a link to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What steps are needed to insert a new node at the beginning of a doubly linked list?
1. Create a new node with the given data.<br>2. Set the new node's next link to the current head.<br>3. Set the new node's previous link to None.<br>4. If the list is not empty, set the current head's previous link to the new node.<br>5. Update the head to be the new node.
Click to reveal answer
intermediate
Why do we need to update the previous link of the old head node when inserting at the beginning?
Because the new node becomes the first node, the old head now comes after it. Its previous link must point back to the new node to keep the list connected both ways.
Click to reveal answer
beginner
What happens if the doubly linked list is empty when inserting at the beginning?
The new node becomes the only node in the list. Its next and previous links are both None, and the head points to this new node.
Click to reveal answer
beginner
Show the Python code snippet to insert a node at the beginning of a doubly linked list.
def insert_at_beginning(head, data):
new_node = Node(data)
new_node.next = head
new_node.prev = None
if head is not None:
head.prev = new_node
head = new_node
return head
Click to reveal answer
What link of the new node should be set to None when inserting at the beginning?
✗ Incorrect
The new node is the first node, so its previous link must be None. Its next link points to the old head.
When inserting at the beginning, what must be updated in the old head node?
✗ Incorrect
The old head's previous link must point to the new node to maintain the backward connection.
If the list is empty, what will be the next link of the new node after insertion at beginning?
✗ Incorrect
With no nodes, the new node's next link is None because there is no next node.
What is the time complexity of inserting a node at the beginning of a doubly linked list?
✗ Incorrect
Insertion at the beginning is done by changing a few pointers, so it takes constant time.
Which pointer in the doubly linked list node points to the next node?
✗ Incorrect
The 'next' pointer points to the next node in the list.
Explain the process of inserting a new node at the beginning of a doubly linked list.
Think about how the links change to keep the list connected both ways.
You got /5 concepts.
Describe how the doubly linked list changes when inserting at the beginning if the list is empty versus not empty.
Consider the links of the new node and the old head in both cases.
You got /2 concepts.