Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a struct for a doubly linked list node.
DSA Go
type Node struct {
value int
prev *[1]
next *Node
}Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'node' which is undefined.
Using a different struct name like 'List' or 'Element' which does not match the struct.
✗ Incorrect
The prev pointer should point to the same Node struct type, so *Node is correct.
2fill in blank
mediumComplete the code to initialize a new doubly linked list node with a value.
DSA Go
func NewNode(val int) *Node {
return &Node{value: [1], prev: nil, next: nil}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'val' which is undefined in this scope.
Trying to access val.value which is invalid because val is an int.
✗ Incorrect
The function parameter is named val, so we assign value: val to initialize the node's value.
3fill in blank
hardFix the error in the code to link two nodes correctly in a doubly linked list.
DSA Go
func LinkNodes(first, second *Node) {
first.next = [1]
second.prev = first
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting first.next to first itself causes a cycle.
Setting first.next to nil breaks the link.
✗ Incorrect
The first node's next pointer should point to the second node to link them.
4fill in blank
hardFill both blanks to create a function that inserts a new node after a given node.
DSA Go
func InsertAfter(node, newNode *Node) {
newNode.prev = [1]
newNode.next = [2]
if node.next != nil {
node.next.prev = newNode
}
node.next = newNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping prev and next assignments.
Setting newNode.next to newNode or nil incorrectly.
✗ Incorrect
The new node's prev points to the current node, and next points to current node's next.
5fill in blank
hardFill all three blanks to create a function that removes a node from the doubly linked list.
DSA Go
func RemoveNode(node *Node) {
if node.prev != nil {
node.prev.next = [1]
}
if node.next != nil {
node.next.prev = [2]
}
node.prev = [3]
node.next = nil
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting node.prev or node.next to itself instead of nil.
Not updating the neighboring nodes' pointers correctly.
✗ Incorrect
To remove a node, link its previous node's next to its next node, and next node's prev to its prev node. Then clear node's prev and next.
