Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the base case for the recursive reverse function.
DSA Go
func reverseList(head *ListNode) *ListNode {
if head == nil || head.[1] == nil {
return head
}
// recursive call
return nil
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes a compile error because 'prev' is not a field.
Not checking for nil causes runtime errors.
✗ Incorrect
The base case checks if the current node is nil or if its next pointer is nil, meaning it's the last node.
2fill in blank
mediumComplete the code to recursively reverse the rest of the list after the head.
DSA Go
func reverseList(head *ListNode) *ListNode {
if head == nil || head.next == nil {
return head
}
newHead := reverseList(head.[1])
return newHead
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes compile errors.
Calling reverseList on head causes infinite recursion.
✗ Incorrect
The recursive call is made on head.next to reverse the rest of the list.
3fill in blank
hardFix the error in the code to correctly reverse the link between nodes.
DSA Go
func reverseList(head *ListNode) *ListNode {
if head == nil || head.next == nil {
return head
}
newHead := reverseList(head.next)
head.next.next = head
head.[1] = nil
return newHead
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.prev causes compile errors.
Not setting head.next to nil causes infinite loops.
✗ Incorrect
After reversing, head.next should be set to nil to avoid cycles.
4fill in blank
hardFill both blanks to complete the recursive reversal and return the new head.
DSA Go
func reverseList(head *ListNode) *ListNode {
if head == nil || head.next == nil {
return head
}
newHead := reverseList(head.[1])
head.next.[2] = head
head.next = nil
return newHead
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors.
Not setting head.next to nil causes cycles.
✗ Incorrect
The recursive call is on head.next, and head.next.next is set to head to reverse the link.
5fill in blank
hardFill all three blanks to complete the recursive reverse function with proper pointer updates.
DSA Go
func reverseList(head *ListNode) *ListNode {
if head == nil || head.[1] == nil {
return head
}
newHead := reverseList(head.[2])
head.next.[3] = head
head.next = nil
return newHead
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' causes compile errors.
Not setting head.next to nil causes infinite loops.
✗ Incorrect
All blanks use 'next' to correctly traverse and reverse the singly linked list.
