Bird
Raised Fist0
DSA Goprogramming~10 mins

Reorder Linked List in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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
}
Drag options to blanks, or click blank then click option'
ANext
BPrev
CHead
DTail
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
2fill in blank
medium

Complete 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
}
Drag options to blanks, or click blank then click option'
AHead
BPrev
CNext
DTail
Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next to get the next node
Not saving the next node before reversing links
3fill in blank
hard

Fix 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
}
Drag options to blanks, or click blank then click option'
Atmp1
Bfirst
Ctmp2
Dsecond
Attempts:
3 left
💡 Hint
Common Mistakes
Linking second.Next to second or tmp2 causing cycles
Linking second.Next to first causing incorrect order
4fill in blank
hard

Fill 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
    }
}
Drag options to blanks, or click blank then click option'
ANext
BPrev
Ctmp1
Dtmp2
Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next for slow pointer
Linking second.Next to tmp2 causing wrong order
5fill in blank
hard

Fill 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++
}
Drag options to blanks, or click blank then click option'
ANext
B%
Cindex
DPrev
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

Practice

(1/5)
1. What is the main goal of the Reorder Linked List problem?
easy
A. Rearrange the list so nodes alternate from start and end towards the center
B. Sort the linked list in ascending order
C. Reverse the entire linked list
D. Remove duplicate nodes from the linked list

Solution

  1. Step 1: Understand the problem statement

    The problem asks to reorder the list so that nodes come alternately from the start and the end, like L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 and so on.
  2. Step 2: Differentiate from other operations

    This is not sorting or reversing the entire list, but a specific pattern of rearrangement.
  3. Final Answer:

    Rearrange the list so nodes alternate from start and end towards the center -> Option A
  4. Quick Check:

    Reorder = alternate start/end nodes [OK]
Hint: Think of weaving nodes from front and back alternately [OK]
Common Mistakes:
  • Confusing reorder with sorting
  • Trying to reverse the whole list only
  • Ignoring the alternating pattern
2. Which of the following Go code snippets correctly declares a singly linked list node struct for the reorder linked list problem?
easy
A. class ListNode { int val; ListNode next; }
B. type ListNode struct { Val int; Next *ListNode }
C. struct ListNode { int Val; ListNode* Next; }
D. type ListNode struct { Value int; NextNode *ListNode }

Solution

  1. Step 1: Identify Go struct syntax

    In Go, structs are declared with the keyword type, fields have names and types separated by spaces, and pointers use *.
  2. Step 2: Match correct field names and types

    type ListNode struct { Val int; Next *ListNode } uses Val int and Next *ListNode, which is the standard for singly linked list nodes in Go.
  3. Final Answer:

    type ListNode struct { Val int; Next *ListNode } -> Option B
  4. Quick Check:

    Go struct with pointer field = type ListNode struct { Val int; Next *ListNode } [OK]
Hint: Remember Go struct fields: name type; pointer uses * [OK]
Common Mistakes:
  • Using C/C++ or Java syntax in Go
  • Missing pointer * for next node
  • Incorrect field names or types
3. Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, what is the reordered list after applying the reorder linked list algorithm?
medium
A. 1 -> 5 -> 2 -> 4 -> 3 -> null
B. 5 -> 4 -> 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> 4 -> 5 -> null
D. 1 -> 3 -> 5 -> 2 -> 4 -> null

Solution

  1. Step 1: Identify the reorder pattern

    The reorder pattern takes the first node, then the last node, then second node, then second last node, and so on.
  2. Step 2: Apply pattern to given list

    Start: 1, end: 5, then 2, then 4, then 3 in the middle. So the reordered list is 1 -> 5 -> 2 -> 4 -> 3 -> null.
  3. Final Answer:

    1 -> 5 -> 2 -> 4 -> 3 -> null -> Option A
  4. Quick Check:

    Alternate front and back nodes = 1 -> 5 -> 2 -> 4 -> 3 -> null [OK]
Hint: Pick nodes alternately from front and back [OK]
Common Mistakes:
  • Reversing the entire list instead
  • Keeping original order unchanged
  • Mixing node order incorrectly
4. Consider this Go function snippet for reordering a linked list:
func reorderList(head *ListNode) {
    if head == nil || head.Next == nil { return }
    slow, fast := head, head
    for fast != nil && fast.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
    }
    second := slow.Next
    slow.Next = nil
    var prev *ListNode
    for second != nil {
        next := second.Next
        second.Next = prev
        prev = second
        second = next
    }
    first, second = head, prev
    for second != nil {
        tmp1 := first.Next
        tmp2 := second.Next
        first.Next = second
        second.Next = tmp1
        first = tmp1
        second = tmp2
    }
}
What is the bug in this code?
medium
A. The function does not handle empty or single-node lists
B. The middle node is not correctly found; slow pointer should start at head.Next
C. The loop to reverse the second half does not update the prev pointer correctly
D. The reversed second half is not properly linked back to the first half

Solution

  1. Step 1: Analyze the reversal and merging steps

    The code correctly finds the middle, reverses the second half, and then merges nodes alternately.
  2. Step 2: Identify the bug in linking halves

    After merging, the last node of the reordered list may still point to old nodes if not handled carefully. The code misses ensuring the last node's Next is nil, causing potential cycle or dangling links.
  3. Final Answer:

    The reversed second half is not properly linked back to the first half -> Option D
  4. Quick Check:

    Linking halves correctly is key [OK]
Hint: Check if last node's Next is set to nil after merge [OK]
Common Mistakes:
  • Not breaking the list at middle properly
  • Forgetting to set slow.Next = nil
  • Incorrect pointer updates in merge loop
5. You have a linked list with nodes containing values 1 -> 2 -> 3 -> null. After reordering, what will be the list if you apply the reorder linked list algorithm twice in a row?
hard
A. 1 -> 3 -> 2 -> null (same as after first reorder)
B. 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> null (original list)
D. 2 -> 3 -> 1 -> null

Solution

  1. Step 1: Apply reorder once

    First reorder transforms 1->2->3 into 1->3->2.
  2. Step 2: Apply reorder again on the new list

    Reordering 1->3->2 again returns the original order 1->2->3 because the reorder pattern is symmetric and reverses itself on second application.
  3. Final Answer:

    1 -> 2 -> 3 -> null (original list) -> Option C
  4. Quick Check:

    Reorder twice returns original list [OK]
Hint: Reordering twice restores original order [OK]
Common Mistakes:
  • Assuming reorder twice keeps the first reorder result
  • Not recognizing reorder symmetry
  • Confusing reorder with reverse
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard