Bird
0
0
DSA Cprogramming~20 mins

Sort a Linked List Using Merge Sort in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Merge Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Merge Sort on a Linked List
What is the printed state of the linked list after applying merge sort on the following list?

Initial list: 4 -> 2 -> 1 -> 3 -> null
DSA C
struct Node {
    int data;
    struct Node* next;
};

// Assume mergeSort and helper functions are correctly implemented
// After mergeSort is called on the head of the list
// The list is printed using printList function

// Initial list: 4 -> 2 -> 1 -> 3 -> null
// After mergeSort, what is the output?
A1 -> 2 -> 3 -> 4 -> null
B4 -> 3 -> 2 -> 1 -> null
C1 -> 3 -> 2 -> 4 -> null
D2 -> 1 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Merge sort splits the list and merges sorted halves.
🧠 Conceptual
intermediate
1:30remaining
Why Use Merge Sort for Linked Lists?
Why is merge sort preferred over quicksort for sorting a linked list?
ABecause merge sort uses less memory than quicksort on arrays
BBecause merge sort does not require random access and works well with linked lists
CBecause quicksort is faster on linked lists
DBecause quicksort cannot be implemented on linked lists
Attempts:
2 left
💡 Hint
Think about how linked lists store data and how merge sort accesses elements.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Merge Sort Implementation
What error will this code cause when sorting a linked list using merge sort?

Code snippet:
struct Node* mergeSort(struct Node* head) { if (head == NULL || head->next == NULL) { return head; } struct Node* middle = getMiddle(head); struct Node* nextOfMiddle = middle->next; middle->next = NULL; struct Node* left = mergeSort(head); struct Node* right = mergeSort(nextOfMiddle); struct Node* sortedList = sortedMerge(left, right); return sortedList; } // getMiddle returns the middle node of the list // sortedMerge merges two sorted lists
ASegmentation fault due to incorrect middle node calculation
BInfinite recursion because base case is missing
CNo error, code runs correctly
DMemory leak due to not freeing nodes
Attempts:
2 left
💡 Hint
Check how getMiddle is implemented and what happens if middle is NULL.
📝 Syntax
advanced
1:30remaining
Correct the Syntax Error in Merge Function
Which option correctly fixes the syntax error in this merge function snippet?

Code snippet:
struct Node* sortedMerge(struct Node* a, struct Node* b) { struct Node* result = NULL; if (a == NULL) return b else if (b == NULL) return a; if (a->data <= b->data) { result = a; result->next = sortedMerge(a->next, b); } else { result = b; result->next = sortedMerge(a, b->next); } return result; }
AReplace 'result = a;' with 'result == a;'
BChange 'else if' to 'if' in second condition
CAdd semicolon after 'return b' line
DRemove 'return result;' line
Attempts:
2 left
💡 Hint
Check missing punctuation after return statements.
🚀 Application
expert
1:00remaining
Time Complexity of Merge Sort on Linked List
What is the time complexity of merge sort when sorting a linked list with n nodes?
AO(n^2)
BO(log n)
CO(n)
DO(n log n)
Attempts:
2 left
💡 Hint
Consider how merge sort divides and merges the list.