Bird
Raised Fist0
DSA Cprogramming~10 mins

Sort a Linked List Using Merge Sort in DSA C - 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.

DSA C
struct Node* findMiddle(struct Node* head) {
    struct Node* slow = head;
    struct Node* fast = head->next;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->[1];
        fast = fast->next->next;
    }
    return slow;
}
Drag options to blanks, or click blank then click option'
Achild
Bprev
Cnext
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for moving forward.
Using 'fast' pointer instead of 'slow' for single steps.
2fill in blank
medium

Complete the code to merge two sorted linked lists.

DSA C
struct Node* merge(struct Node* a, struct Node* b) {
    if (a == NULL) return b;
    if (b == NULL) return a;
    struct Node* result;
    if (a->data <= b->data) {
        result = a;
        result->next = merge(a->next, [1]);
    } else {
        result = b;
        result->next = merge(a, b->next);
    }
    return result;
}
Drag options to blanks, or click blank then click option'
Aa
Bb
Cresult
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'a' instead of 'b' in the recursive call.
Passing NULL instead of the other list.
3fill in blank
hard

Fix the error in the mergeSort function to correctly split the list.

DSA C
struct Node* mergeSort(struct Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct Node* middle = findMiddle(head);
    struct Node* nextOfMiddle = middle->[1];
    middle->next = NULL;
    struct Node* left = mergeSort(head);
    struct Node* right = mergeSort(nextOfMiddle);
    struct Node* sortedList = merge(left, right);
    return sortedList;
}
Drag options to blanks, or click blank then click option'
Arandom
Bprev
Cchild
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to get the next node.
Not setting middle->next to NULL to split the list.
4fill in blank
hard

Fill both blanks to complete the merge function's base cases.

DSA C
struct Node* merge(struct Node* a, struct Node* b) {
    if (a == [1]) return b;
    if (b == [2]) return a;
    // rest of merge code
}
Drag options to blanks, or click blank then click option'
ANULL
B0
Chead
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or false instead of NULL for pointers.
Checking against 'head' which is undefined here.
5fill in blank
hard

Fill all three blanks to complete the mergeSort function call and return.

DSA C
struct Node* mergeSort(struct Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct Node* middle = findMiddle(head);
    struct Node* nextOfMiddle = middle->next;
    middle->next = NULL;
    struct Node* left = mergeSort([1]);
    struct Node* right = mergeSort([2]);
    struct Node* sortedList = merge([3], right);
    return sortedList;
}
Drag options to blanks, or click blank then click option'
Ahead
BnextOfMiddle
Cleft
Dmiddle
Attempts:
3 left
💡 Hint
Common Mistakes
Passing middle instead of nextOfMiddle for right half.
Merging unsorted lists instead of sorted ones.

Practice

(1/5)
1. What is the main advantage of using merge sort to sort a linked list compared to quicksort?
easy
A. Merge sort does not require random access, which suits linked lists well.
B. Quicksort uses less memory than merge sort for linked lists.
C. Merge sort is faster than quicksort on arrays.
D. Quicksort is stable while merge sort is not.

Solution

  1. Step 1: Understand linked list access

    Linked lists do not support fast random access, unlike arrays.
  2. Step 2: Compare sorting algorithms

    Merge sort works well with sequential access, while quicksort relies on random access for efficiency.
  3. Final Answer:

    Merge sort does not require random access, which suits linked lists well. -> Option A
  4. Quick Check:

    Linked list + merge sort = no random access needed [OK]
Hint: Merge sort fits linked lists due to sequential access [OK]
Common Mistakes:
  • Thinking quicksort is always better
  • Assuming merge sort needs random access
  • Confusing stability of sorting algorithms
2. Which of the following is the correct way to split a linked list into two halves for merge sort in C?
easy
A. Split by dividing the node values by 2.
B. Use two pointers: slow moves 1 step, fast moves 2 steps; slow marks mid.
C. Use a single pointer to count nodes and split at half count.
D. Use recursion without splitting the list.

Solution

  1. Step 1: Identify splitting technique

    The fast and slow pointer method finds the middle node efficiently.
  2. Step 2: Understand pointer movement

    Slow pointer moves one node at a time, fast pointer moves two nodes; when fast reaches end, slow is at mid.
  3. Final Answer:

    Use two pointers: slow moves 1 step, fast moves 2 steps; slow marks mid. -> Option B
  4. Quick Check:

    Fast/slow pointers split linked list [OK]
Hint: Use fast/slow pointers to find middle node [OK]
Common Mistakes:
  • Trying to split by node values
  • Using only one pointer to split
  • Not stopping fast pointer correctly
3. Given the linked list: 4 -> 2 -> 1 -> 3 -> NULL, what is the sorted list after applying merge sort?
medium
A. 4 -> 3 -> 2 -> 1 -> NULL
B. 1 -> 3 -> 2 -> 4 -> NULL
C. 2 -> 1 -> 3 -> 4 -> NULL
D. 1 -> 2 -> 3 -> 4 -> NULL

Solution

  1. Step 1: Break list into halves recursively

    Split into (4 -> 2) and (1 -> 3), then further split until single nodes.
  2. Step 2: Merge sorted halves

    Merge nodes in order: 1, 2, 3, 4 to get sorted list.
  3. Final Answer:

    1 -> 2 -> 3 -> 4 -> NULL -> Option D
  4. Quick Check:

    Sorted ascending order = 1,2,3,4 [OK]
Hint: Merge sort outputs ascending sorted list [OK]
Common Mistakes:
  • Confusing merge order
  • Not splitting correctly
  • Assuming original order remains
4. In the following C code snippet for merge sort on linked list, what is the error?
Node* mergeSort(Node* head) {
  if (!head || !head->next) return head;
  Node* slow = head;
  Node* fast = head;
  while (fast && fast->next) {
    slow = slow->next;
    fast = fast->next->next;
  }
  Node* mid = slow->next;
  slow->next = NULL;
  Node* left = mergeSort(head);
  Node* right = mergeSort(mid);
  return merge(left, right);
}
medium
A. The base case should check only for head->next, not head.
B. Slow pointer should move two steps each iteration.
C. Fast pointer should start at head->next, not head.
D. The function should not split the list.

Solution

  1. Step 1: Analyze pointer initialization

    Fast pointer should start at head->next to correctly find middle for even nodes.
  2. Step 2: Understand pointer movement impact

    Starting fast at head skips the first node, causing incorrect mid calculation.
  3. Final Answer:

    Fast pointer should start at head->next, not head. -> Option C
  4. Quick Check:

    Fast pointer start affects mid node [OK]
Hint: Start fast pointer at head->next for correct mid [OK]
Common Mistakes:
  • Starting fast at head
  • Moving slow pointer incorrectly
  • Ignoring base case conditions
5. You have a linked list with duplicate values: 5 -> 3 -> 8 -> 5 -> 2 -> NULL. After applying merge sort, which of the following is the correct sorted list?
hard
A. 2 -> 3 -> 5 -> 5 -> 8 -> NULL
B. 5 -> 3 -> 8 -> 5 -> 2 -> NULL
C. 2 -> 3 -> 8 -> 5 -> 5 -> NULL
D. 3 -> 2 -> 5 -> 5 -> 8 -> NULL

Solution

  1. Step 1: Understand merge sort stability

    Merge sort preserves order of equal elements, so duplicates stay in relative order.
  2. Step 2: Sort values ascending

    Sorted order is 2, 3, 5, 5, 8 with duplicates adjacent.
  3. Final Answer:

    2 -> 3 -> 5 -> 5 -> 8 -> NULL -> Option A
  4. Quick Check:

    Sorted with duplicates preserved = 2,3,5,5,8 [OK]
Hint: Merge sort keeps duplicates in order [OK]
Common Mistakes:
  • Assuming duplicates get removed
  • Sorting duplicates incorrectly
  • Mixing order of duplicates
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