Bird
Raised Fist0
DSA Goprogramming~30 mins

Reorder Linked List in DSA Go - Build from Scratch

Choose your learning style10 modes available
Reorder Linked List
📖 Scenario: You are working on a program that manages a linked list of tasks. Sometimes, you want to reorder the list so that the first task is followed by the last task, then the second task, then the second last task, and so on. This helps to balance urgent and less urgent tasks visually.
🎯 Goal: Build a Go program that reorders a singly linked list in the pattern: first node, last node, second node, second last node, and so on.
📋 What You'll Learn
Create a singly linked list with nodes containing integer values
Find the middle of the linked list
Reverse the second half of the linked list
Merge the two halves by alternating nodes
Print the reordered linked list
💡 Why This Matters
🌍 Real World
Reordering linked lists is useful in scheduling tasks, balancing workloads, or rearranging data for better access patterns.
💼 Career
Understanding linked list manipulation is important for software engineers working on low-level data structures, system programming, and performance optimization.
Progress0 / 4 steps
1
Create the linked list
Create a singly linked list with the nodes containing these exact integer values in order: 1, 2, 3, 4, 5. Use the struct ListNode with fields Val int and Next *ListNode. Create a variable called head pointing to the first node.
DSA Go
Hint

Start by creating the first node with value 1 and assign it to head. Then link each next node with the exact values 2, 3, 4, and 5.

2
Find the middle of the linked list
Create two pointers called slow and fast both starting at head. Move slow by one node and fast by two nodes in a loop until fast reaches the end or fast.Next is nil. This will find the middle node. After the loop, slow should point to the middle node.
DSA Go
Hint

Use a for loop to move slow by one and fast by two nodes until fast or fast.Next is nil.

3
Reverse the second half of the list
Create a variable prev set to nil. Create a variable curr set to slow. Use a loop to reverse the linked list starting from curr until curr is nil. In each iteration, save curr.Next in nextTemp, set curr.Next to prev, then move prev to curr and curr to nextTemp. After the loop, prev will be the head of the reversed second half.
DSA Go
Hint

Use a for loop to reverse the linked list starting from slow. Remember to keep track of the next node before changing links.

4
Merge the two halves and print the reordered list
Create two pointers: first starting at head and second starting at prev (the head of reversed second half). Use a loop while second is not nil. In each iteration, save first.Next in tmp1 and second.Next in tmp2. Set first.Next to second, then second.Next to tmp1. Move first to tmp1 and second to tmp2. After merging, print the reordered list nodes' values separated by arrows like 1 -> 5 -> 2 -> 4 -> 3 -> nil.
DSA Go
Hint

Alternate nodes from the first half and reversed second half. Then print the list values separated by arrows and ending with nil.

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