Bird
Raised Fist0
DSA C#programming~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#
ListNode FindMiddle(ListNode head) {
    ListNode slow = head;
    ListNode fast = head;
    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'
Ahead
Bprev
Cnext
Dnull
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 merge two sorted linked lists.

DSA C#
ListNode Merge(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode tail = dummy;
    while (l1 != null && l2 != null) {
        if (l1.val < l2.val) {
            tail.next = l1;
            l1 = l1.[1];
        } else {
            tail.next = l2;
            l2 = l2.next;
        }
        tail = tail.next;
    }
    tail.next = (l1 != null) ? l1 : l2;
    return dummy.next;
}
Drag options to blanks, or click blank then click option'
Aprev
Bnext
Chead
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using l1.val instead of l1.next to move the pointer.
Using prev which is not defined in singly linked list.
3fill in blank
hard

Fix the error in the recursive merge sort call to sort the right half.

DSA C#
ListNode MergeSort(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode mid = FindMiddle(head);
    ListNode rightHead = mid.next;
    mid.next = null;
    ListNode left = MergeSort(head);
    ListNode right = MergeSort([1]);
    return Merge(left, right);
}
Drag options to blanks, or click blank then click option'
ArightHead
Bmid
Cnull
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of rightHead for the right half.
Using head again which is the start of the left half.
4fill in blank
hard

Fill both blanks to correctly split the linked list into two halves.

DSA C#
ListNode FindMiddle(ListNode head) {
    ListNode slow = head;
    ListNode fast = head.[1];
    while (fast != null && fast.[2] != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Attempts:
3 left
💡 Hint
Common Mistakes
Starting fast at head instead of head.next.
Using 'prev' which is not defined in singly linked list.
5fill in blank
hard

Fill both blanks to complete the merge sort implementation for linked list.

DSA C#
public ListNode SortList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode mid = FindMiddle(head);
    ListNode rightHead = mid.[1];
    mid.[2] = null;
    ListNode left = SortList(head);
    ListNode right = SortList(rightHead);
    return Merge(left, right);
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not defined in singly linked list.
Not breaking the list by setting mid.next to null.

Practice

(1/5)
1. What is the main advantage of using merge sort to sort a linked list compared to other sorting algorithms?
easy
A. It efficiently sorts the list in O(n log n) time without needing extra space for arrays.
B. It sorts the list in O(n^2) time but uses less memory.
C. It requires the list to be converted into an array first.
D. It only works for linked lists with integer values.

Solution

  1. Step 1: Understand merge sort time complexity on linked lists

    Merge sort divides the list into halves recursively and merges them, achieving O(n log n) time.
  2. Step 2: Compare space usage with other sorts

    Unlike quicksort or heapsort on arrays, merge sort on linked lists does not require extra array space, making it memory efficient.
  3. Final Answer:

    It efficiently sorts the list in O(n log n) time without needing extra space for arrays. -> Option A
  4. Quick Check:

    Merge sort = O(n log n) + memory efficient [OK]
Hint: Merge sort is best for linked lists due to O(n log n) and no extra arrays [OK]
Common Mistakes:
  • Thinking merge sort uses extra array space for linked lists
  • Confusing merge sort with bubble sort time complexity
  • Assuming merge sort only works on arrays
  • Believing merge sort requires integer-only lists
2. Which of the following is the correct way to split a linked list into two halves in C# for merge sort?
easy
A. Convert the list to an array and split the array.
B. Use a single pointer to count nodes and split at half index.
C. Use two pointers: slow moves 1 step, fast moves 2 steps; slow marks middle.
D. Split the list by removing the last node repeatedly.

Solution

  1. Step 1: Identify the standard technique to find middle in linked list

    Using two pointers, slow and fast, where fast moves twice as fast, slow ends at middle.
  2. Step 2: Explain why other options are incorrect

    Counting nodes requires extra traversal; converting to array is inefficient; removing last node is not splitting in half.
  3. Final Answer:

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

    Two-pointer technique = correct split [OK]
Hint: Use slow-fast pointers to find middle in one pass [OK]
Common Mistakes:
  • Trying to split by counting nodes first
  • Converting linked list to array unnecessarily
  • Removing nodes instead of splitting
  • Using only one pointer to find middle
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 down the list and sort halves recursively

    Split into (4 -> 2) and (1 -> 3). Sort each half: (2 -> 4) and (1 -> 3).
  2. Step 2: Merge the two sorted halves

    Merge (2 -> 4) and (1 -> 3) to get (1 -> 2 -> 3 -> 4).
  3. Final Answer:

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

    Sorted ascending = 1,2,3,4 [OK]
Hint: Merge sorted halves step-by-step to get final sorted list [OK]
Common Mistakes:
  • Not sorting halves before merging
  • Merging without sorting causing wrong order
  • Confusing node values during merge
  • Forgetting to set last node's next to null
4. In the following C# merge sort code for linked list, what is the bug?
public ListNode MergeSort(ListNode head) {
  if (head == null || head.next == null) return head;
  ListNode mid = GetMiddle(head);
  ListNode left = MergeSort(head);
  ListNode right = MergeSort(mid);
  return Merge(left, right);
}
medium
A. The base case is incorrect; it should check for head.next.next.
B. The middle node is not disconnected from the left half, causing infinite recursion.
C. The Merge function is called before sorting the halves.
D. The GetMiddle function returns the last node instead of the middle.

Solution

  1. Step 1: Identify the role of splitting the list

    After finding middle, the list must be split by setting middle's previous node next to null to separate halves.
  2. Step 2: Explain the bug effect

    Without disconnecting, recursive calls keep processing the same list, causing infinite recursion or stack overflow.
  3. Final Answer:

    The middle node is not disconnected from the left half, causing infinite recursion. -> Option B
  4. Quick Check:

    Disconnect middle to avoid infinite recursion [OK]
Hint: Always break list into two by setting middle's previous next to null [OK]
Common Mistakes:
  • Not disconnecting the list at middle
  • Wrong base case conditions
  • Calling merge before sorting halves
  • Assuming GetMiddle returns last node
5. You have a linked list with nodes containing duplicate values: 3 -> 1 -> 2 -> 3 -> 2 -> null. After applying merge sort, what will be the correct sorted linked list?
hard
A. 1 -> 2 -> 2 -> 3 -> 3 -> null
B. 3 -> 3 -> 2 -> 2 -> 1 -> null
C. 1 -> 3 -> 2 -> 3 -> 2 -> null
D. 2 -> 1 -> 3 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand merge sort handles duplicates by sorting all nodes

    Merge sort compares node values and merges in ascending order, preserving duplicates in sorted order.
  2. Step 2: Sort the list step-by-step

    Sorted order is 1, 2, 2, 3, 3, so the final list is 1 -> 2 -> 2 -> 3 -> 3 -> null.
  3. Final Answer:

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

    Duplicates sorted ascending = 1,2,2,3,3 [OK]
Hint: Merge sort keeps duplicates in sorted order naturally [OK]
Common Mistakes:
  • Assuming duplicates get removed
  • Mixing order of duplicate nodes
  • Not sorting duplicates correctly
  • Confusing stable sort with unstable sort
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