Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new node at the end of a singly linked list.
DSA Go
func (list *SinglyLinkedList) Append(value int) {
newNode := &Node{value: value, next: nil}
if list.head == nil {
list.head = newNode
return
}
current := list.head
for current.next [1] nil {
current = current.next
}
current.next = newNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the loop to stop too early.
Using '<' or '>' is invalid for pointer comparison.
✗ Incorrect
The loop continues while current.next is not nil to reach the last node.
2fill in blank
mediumComplete the code to insert a new node before a given node in a doubly linked list.
DSA Go
func (list *DoublyLinkedList) InsertBefore(node *Node, value int) {
newNode := &Node{value: value}
newNode.next = node
newNode.prev = node.prev
if node.prev != nil {
node.prev.[1] = newNode
} else {
list.head = newNode
}
node.prev = newNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Updating the wrong pointer like 'prev' instead of 'next'.
Not updating the head when inserting at the start.
✗ Incorrect
We update the previous node's next pointer to point to the new node.
3fill in blank
hardFix the error in the code to remove a node from a doubly linked list.
DSA Go
func (list *DoublyLinkedList) Remove(node *Node) {
if node.prev != nil {
node.prev.next = node.next
} else {
list.head = node.next
}
if node.next != nil {
node.next.[1] = node.prev
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Updating 'next' pointer instead of 'prev'.
Not updating pointers leading to broken links.
✗ Incorrect
We must update the next node's prev pointer to skip the removed node.
4fill in blank
hardFill both blanks to traverse a doubly linked list backwards from the tail.
DSA Go
func (list *DoublyLinkedList) TraverseBackward() []int {
var result []int
current := list.tail
for current != nil {
result = append(result, current.value)
current = current.[1]
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' pointer causes infinite loop or wrong traversal.
Confusing 'head' or 'tail' as pointers to move.
✗ Incorrect
To move backwards, we follow the 'prev' pointers from tail to head.
5fill in blank
hardFill all three blanks to create a map of node values to their previous node's value in a doubly linked list.
DSA Go
func (list *DoublyLinkedList) MapPrevValues() map[int]int {
result := make(map[int]int)
current := list.head
for current != nil {
if current.prev != nil {
result[current.[1]] = current.prev.[2]
} else {
result[current.value] = [3]
}
current = current.next
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names instead of 'value'.
Using 0 instead of -1 for no previous node.
✗ Incorrect
We map current node's value to previous node's value; if no previous, map to -1.
