Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle node of the linked list using slow and fast pointers.
DSA Go
slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.[1] fast = fast.Next.Next }
Attempts:
3 left
💡 Hint
Common Mistakes
Using fast.Next instead of slow.Next for the slow pointer
Using Prev which is not defined in singly linked list
✗ Incorrect
To move the slow pointer one step forward, we use slow.Next. This helps find the middle node.
2fill in blank
mediumComplete the code to reverse the second half of the linked list starting from the middle node.
DSA Go
var prev *ListNode = nil curr := slow for curr != nil { nextTemp := curr.[1] curr.Next = prev prev = curr curr = nextTemp }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next to get the next node
Not saving the next node before reversing links
✗ Incorrect
To reverse the list, we need to move forward using curr.Next to save the next node before changing links.
3fill in blank
hardFix the error in the code that merges two halves of the list alternately.
DSA Go
first, second := head, prev for second != nil { tmp1 := first.Next tmp2 := second.Next first.Next = second second.Next = [1] first = tmp1 second = tmp2 }
Attempts:
3 left
💡 Hint
Common Mistakes
Linking second.Next to second or tmp2 causing cycles
Linking second.Next to first causing incorrect order
✗ Incorrect
After linking second after first, second.Next should point to tmp1 to continue merging alternately.
4fill in blank
hardFill both blanks to complete the function that reorders the linked list.
DSA Go
func reorderList(head *ListNode) {
if head == nil || head.Next == nil {
return
}
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.[1]
fast = fast.Next.Next
}
prev := reverseList(slow)
first := head
second := prev
for second != nil {
tmp1 := first.Next
tmp2 := second.Next
first.Next = second
second.Next = [2]
first = tmp1
second = tmp2
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next for slow pointer
Linking second.Next to tmp2 causing wrong order
✗ Incorrect
The slow pointer moves using Next to find the middle. When merging, second.Next should point to tmp1 to maintain order.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps node values to their positions if value is even.
DSA Go
positions := map[int]int{}
index := 0
for node := head; node != nil; node = node.[1] {
if node.Val [2] 2 == 0 {
positions[node.Val] = [3]
}
index++
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev which is undefined in singly linked list
Using incorrect operator instead of %
Storing node.Val instead of index
✗ Incorrect
Traverse using Next, check even values using %, and store the current index.
