Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given data.
DSA Go
newNode := &Node{data: [1]Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the function like 'value' or 'item'.
Forgetting to assign the data to the new node.
✗ Incorrect
The new node's data field should be assigned the variable 'data' passed to the function.
2fill in blank
mediumComplete the code to link the new node's next pointer to the current head.
DSA Go
newNode.next = [1]Attempts:
3 left
💡 Hint
Common Mistakes
Linking to tail instead of head.
Linking next to nil which breaks the list.
✗ Incorrect
The new node's next pointer should point to the current head node.
3fill in blank
hardFix the error in setting the previous pointer of the old head node.
DSA Go
if head != nil { head.[1] = newNode }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.next instead of head.prev.
Assigning head.data which is incorrect.
✗ Incorrect
The previous pointer of the old head should point back to the new node.
4fill in blank
hardFill both blanks to update the head pointer and return it.
DSA Go
head = [1] return [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning oldHead instead of updated head.
Assigning head to nil which breaks the list.
✗ Incorrect
The head pointer should be updated to the new node and returned.
5fill in blank
hardFill all three blanks to complete the InsertAtBeginning function for a doubly linked list.
DSA Go
func InsertAtBeginning(head *Node, data int) *Node {
newNode := &Node{data: [1]
newNode.next = [2]
if head != nil {
head.prev = newNode
}
head = [3]
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for data or pointers.
Not updating head to newNode before returning.
✗ Incorrect
The function creates a new node with data, links it to the current head, updates the previous pointer, updates head to newNode, and returns head.
