Bird
0
0
DSA Cprogramming~10 mins

Reorder Linked List in DSA C - Interactive Practice

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

Complete the code to move to the next node in the linked list.

DSA C
current = current->[1];
Drag options to blanks, or click blank then click option'
Atail
Bprev
Chead
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Using 'head' or 'tail' which are not pointers of the current node.
2fill in blank
medium

Complete the code to find the middle node of the linked list using slow and fast pointers.

DSA C
while (fast != NULL && fast->[1] != NULL) {
    slow = slow->next;
    fast = fast->next->next;
}
Drag options to blanks, or click blank then click option'
Aprev
Bnext
Chead
Dtail
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for fast pointer movement.
Not checking if fast->next is NULL before accessing fast->next->next.
3fill in blank
hard

Fix the error in the code to reverse the second half of the linked list.

DSA C
struct ListNode* prev = NULL;
struct ListNode* curr = head;
while (curr != NULL) {
    struct ListNode* nextTemp = curr->[1];
    curr->next = prev;
    prev = curr;
    curr = nextTemp;
}
Drag options to blanks, or click blank then click option'
Anext
Bhead
Ctail
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to save the next node.
Not saving the next node before reversing the link.
4fill in blank
hard

Fill both blanks to merge two halves of the linked list alternately.

DSA C
while (second != NULL) {
    struct ListNode* temp1 = first->next;
    struct ListNode* temp2 = second->[1];
    first->next = second;
    second->next = temp1;
    first = temp1;
    second = [2];
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Ctemp2
Dtemp1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Assigning wrong variable to 'second' causing infinite loop.
5fill in blank
hard

Fill all three blanks to complete the reorder list function.

DSA C
void reorderList(struct ListNode* head) {
    if (head == NULL || head->next == NULL) return;

    struct ListNode *slow = head, *fast = head;
    while (fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->[1];
    }

    struct ListNode* second = slow->next;
    slow->next = NULL;

    struct ListNode* prev = NULL;
    struct ListNode* curr = second;
    while (curr != NULL) {
        struct ListNode* nextTemp = curr->[2];
        curr->next = prev;
        prev = curr;
        curr = nextTemp;
    }

    struct ListNode* first = head;
    second = prev;
    while (second != NULL) {
        struct ListNode* temp1 = first->next;
        struct ListNode* temp2 = second->next;
        first->next = second;
        second->next = temp1;
        first = temp1;
        second = [3];
    }
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Dtemp2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' for fast pointer movement.
Not saving 'next' before reversing links.
Assigning wrong variable to 'second' in merge loop.