Bird
Raised Fist0
DSA Cprogramming~20 mins

Insert at End Tail Insert in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Tail Insert in Linked List
What is the printed linked list after inserting 10, 20, and 30 at the end using tail insert?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) {
        return newNode;
    }
    Node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    head = tailInsert(head, 10);
    head = tailInsert(head, 20);
    head = tailInsert(head, 30);
    printList(head);
    return 0;
}
A10 -> 20 -> 30 -> NULL
B30 -> 20 -> 10 -> NULL
CNULL
D10 -> 20 -> NULL
Attempts:
2 left
💡 Hint
Remember that tail insert adds nodes at the end, preserving the order of insertion.
🧠 Conceptual
intermediate
1:00remaining
Number of Nodes After Tail Insertions
If you start with an empty linked list and perform tail insertions of 5 nodes, how many nodes will the list contain?
A0
B4
C6
D5
Attempts:
2 left
💡 Hint
Each tail insertion adds exactly one node to the list.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Tail Insert Function
What error will occur when running this tail insert function code snippet?
DSA C
Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    Node* temp = head;
    if (head == NULL) {
        head = newNode;
    }
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}
AMemory leak due to missing free
BSegmentation fault due to dereferencing NULL pointer
CNo error, works correctly
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Check what happens when head is NULL and you try to access temp->next.
📝 Syntax
advanced
1:30remaining
Correct Syntax for Tail Insert Loop
Which option correctly iterates to the last node in a singly linked list for tail insertion?
Afor (; temp->next != NULL; temp = temp->next);
Bwhile (temp != NULL) { temp = temp->next; }
Cwhile (temp->next != NULL) { temp = temp->next; }
Dwhile (temp->next) { temp = temp->next; }
Attempts:
2 left
💡 Hint
You want to stop at the last node, not go past it.
🚀 Application
expert
3:00remaining
Final Linked List After Multiple Tail Inserts and One Deletion
Given an empty linked list, you perform tail insertions of values 1, 2, 3, 4, 5 in order. Then you delete the node with value 3. What is the printed linked list?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

Node* tailInsert(Node* head, int val) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) {
        return newNode;
    }
    Node* temp = head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    return head;
}

Node* deleteNode(Node* head, int val) {
    if (head == NULL) return NULL;
    if (head->data == val) {
        Node* temp = head->next;
        free(head);
        return temp;
    }
    Node* curr = head;
    while (curr->next != NULL && curr->next->data != val) {
        curr = curr->next;
    }
    if (curr->next != NULL) {
        Node* temp = curr->next;
        curr->next = temp->next;
        free(temp);
    }
    return head;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    head = tailInsert(head, 1);
    head = tailInsert(head, 2);
    head = tailInsert(head, 3);
    head = tailInsert(head, 4);
    head = tailInsert(head, 5);
    head = deleteNode(head, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 4 -> 5 -> NULL
B1 -> 2 -> 3 -> 4 -> 5 -> NULL
C2 -> 4 -> 5 -> NULL
D1 -> 2 -> 4 -> NULL
Attempts:
2 left
💡 Hint
Deleting node with value 3 removes it from the list, others remain in order.

Practice

(1/5)
1. What does the 'Insert at End' operation do in a singly linked list?
easy
A. Adds a new node at the end (tail) of the list
B. Deletes the last node of the list
C. Adds a new node at the beginning (head) of the list
D. Searches for a node at the end of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the end means adding a new node after the current last node.
  2. Step 2: Identify the effect on the list

    The new node becomes the new tail, extending the list by one node.
  3. Final Answer:

    Adds a new node at the end (tail) of the list -> Option A
  4. Quick Check:

    Insert at end = Add node at tail [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insert at end with insert at beginning
  • Thinking it deletes a node
  • Assuming it searches instead of inserts
2. Which of the following is the correct syntax to create a new node in C for tail insertion?
easy
A. newNode = malloc(sizeof(Node));
B. struct Node newNode = malloc(sizeof(struct Node));
C. Node newNode = malloc(sizeof(Node));
D. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

Solution

  1. Step 1: Understand node creation in C

    We use malloc to allocate memory and cast it to pointer of struct Node.
  2. Step 2: Check syntax correctness

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); correctly casts malloc result to (struct Node*) and uses sizeof(struct Node).
  3. Final Answer:

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); -> Option D
  4. Quick Check:

    Correct malloc and cast syntax = struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); [OK]
Hint: Use malloc with (struct Node*) cast for new node [OK]
Common Mistakes:
  • Missing cast to (struct Node*)
  • Using wrong type without struct keyword
  • Assigning malloc to struct variable instead of pointer
3. What will be the output linked list after inserting 4 at the end of the list 1 -> 2 -> 3 -> NULL using tail insertion?
medium
A. 1 -> 2 -> 3 -> NULL
B. 1 -> 2 -> 3 -> 4 -> NULL
C. 1 -> 2 -> 4 -> 3 -> NULL
D. 4 -> 1 -> 2 -> 3 -> NULL

Solution

  1. Step 1: Understand tail insertion effect

    Inserting 4 at the end adds it after 3, the current tail.
  2. Step 2: Visualize the new list

    The list becomes 1 -> 2 -> 3 -> 4 -> NULL after insertion.
  3. Final Answer:

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

    Insert 4 at tail = 1->2->3->4->NULL [OK]
Hint: New node goes after last node, not at front [OK]
Common Mistakes:
  • Inserting at head instead of tail
  • Placing new node in middle
  • Not updating tail pointer
4. Given the following code snippet for tail insertion, what is the error?
void insertAtEnd(struct Node** head, int data) {
  struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  newNode->data = data;
  newNode->next = NULL;
  if (*head == NULL) {
    *head = newNode;
  } else {
    struct Node* temp = *head;
    while (temp->next != NULL) {
      temp = temp->next;
    }
    temp = newNode;
  }
}
medium
A. temp = newNode; does not link newNode to the list
B. Missing malloc for newNode
C. newNode->next should not be NULL
D. Head pointer is not updated when list is empty

Solution

  1. Step 1: Analyze the while loop and insertion

    The code moves temp to last node, but then does temp = newNode; which only changes temp pointer.
  2. Step 2: Identify correct linking

    To link newNode, temp->next should be assigned newNode, not temp itself.
  3. Final Answer:

    temp = newNode; does not link newNode to the list -> Option A
  4. Quick Check:

    Link newNode by temp->next = newNode [OK]
Hint: Assign newNode to temp->next, not temp [OK]
Common Mistakes:
  • Assigning newNode to temp pointer instead of temp->next
  • Forgetting to set newNode->next to NULL
  • Not handling empty list case
5. You have a singly linked list with nodes 10 -> 20 -> 30 -> NULL. You want to insert 40 at the end using tail insertion. Which of the following code snippets correctly performs this insertion and updates the list?
hard
A. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\nnewNode->data = 40;\nnewNode->next = NULL;\nhead = newNode;
B. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\nnewNode->data = 40;\nnewNode->next = head;\nhead = newNode;
C. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\nnewNode->data = 40;\nnewNode->next = NULL;\nstruct Node* temp = head;\nwhile(temp->next != NULL) { temp = temp->next; }\ntemp->next = newNode;
D. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\nnewNode->data = 40;\nnewNode->next = NULL;\nstruct Node* temp = head;\nwhile(temp != NULL) { temp = temp->next; }\ntemp->next = newNode;

Solution

  1. Step 1: Create new node with data 40 and next NULL

    This prepares the new node to be added at the end.
  2. Step 2: Traverse list to last node

    Using while(temp->next != NULL) moves temp to last node (30).
  3. Step 3: Link last node's next to newNode

    temp->next = newNode correctly attaches new node at end.
  4. Final Answer:

    Traverse to last node with while(temp->next != NULL), then temp->next = newNode -> Option C
  5. Quick Check:

    Traverse to tail, then temp->next = newNode [OK]
Hint: Traverse to last node, then link new node at next [OK]
Common Mistakes:
  • Setting newNode->next to head (inserts at front)
  • Not updating last node's next pointer
  • Loop condition allowing temp to become NULL
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