17 march Linked List questions

Apr 13, 2025
Updated 1 day ago
5 min read

Linked List Questions and Solutions in C Programming | Complete Guide

Introduction

A linked list is one of the most important topics in data structures and programming. It is widely used in memory management, dynamic data storage, stacks, queues, and many real-world algorithms. Unlike arrays, linked lists allow dynamic memory allocation, making insertion and deletion operations more efficient.

In this article, you will learn the most commonly asked linked list questions in C programming with simple and practical solutions. These examples cover insertion, deletion, searching, splitting, circular linked lists, loop detection, recursion, and more. If you are preparing for university exams, coding interviews, or improving your understanding of coding concepts, these questions will help you strengthen your fundamentals.


What is a Linked List?

A linked list is a linear data structure where each element is stored inside a node. Every node contains:

  • Data

  • Pointer to the next node

Basic Node Structure in C

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

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void printList(Node* head) {
    Node* temp = head;

    while(temp) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }

    printf("NULL\n");
}

Linked List Insertion Questions

1. Insert a Node Before the First Node

c
void insertBeforeFirst(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = data;
    newNode->next = *head;
    *head = newNode;
}

2. Insert a Node After the First Node

c
void insertAfterFirst(Node* head, int data) {
    if (!head) return;

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = data;
    newNode->next = head->next;
    head->next = newNode;
}

3. Insert a Node After the Last Node

c
void insertAfterLast(Node* head, int data) {
    Node* temp = head;

    while(temp->next)
        temp = temp->next;

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = data;
    newNode->next = NULL;
    temp->next = newNode;
}

4. Insert a Node Before the Last Node

c
void insertBeforeLast(Node* head, int data) {
    if (!head || !head->next) return;

    Node* temp = head;

    while(temp->next->next)
        temp = temp->next;

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = data;
    newNode->next = temp->next;
    temp->next = newNode;
}

5. Search a Node and Insert Before and After It

c
void insertAroundValue(Node** head, int searchVal, int beforeData, int afterData) {
    Node* temp = *head;
    Node* prev = NULL;

    while(temp && temp->data != searchVal) {
        prev = temp;
        temp = temp->next;
    }

    if (temp) {

        Node* newBefore = (Node*)malloc(sizeof(Node));
        newBefore->data = beforeData;
        newBefore->next = temp;

        if (prev)
            prev->next = newBefore;
        else
            *head = newBefore;

        Node* newAfter = (Node*)malloc(sizeof(Node));
        newAfter->data = afterData;
        newAfter->next = temp->next;

        temp->next = newAfter;
    }
}

Linked List Deletion Questions

6. Delete the First Node

c
void deleteFirst(Node** head) {
    if (*head) {
        Node* temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

7. Delete the Last Node

c
void deleteLast(Node** head) {

    if (!*head) return;

    if (!(*head)->next) {
        free(*head);
        *head = NULL;
        return;
    }

    Node* temp = *head;

    while(temp->next->next)
        temp = temp->next;

    free(temp->next);
    temp->next = NULL;
}

8. Search and Delete a Node

c
void deleteValue(Node** head, int value) {

    Node* temp = *head;
    Node* prev = NULL;

    while(temp && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }

    if (!temp) return;

    if (!prev)
        *head = temp->next;
    else
        prev->next = temp->next;

    free(temp);
}

Linked List Splitting Questions

9. Split List into Odd and Even Nodes

c
void splitOddEven(Node* head, Node** odd, Node** even) {

    while(head) {

        Node* newNode = (Node*)malloc(sizeof(Node));

        newNode->data = head->data;
        newNode->next = NULL;

        if (head->data % 2 == 0) {
            newNode->next = *even;
            *even = newNode;
        } else {
            newNode->next = *odd;
            *odd = newNode;
        }

        head = head->next;
    }
}

10. Split a Linked List at a Given Index

c
void splitAtIndex(Node* head, int index, Node** first, Node** second) {

    *first = head;
    int i = 0;

    while(i < index - 1 && head) {
        head = head->next;
        i++;
    }

    if (head) {
        *second = head->next;
        head->next = NULL;
    }
}

Recursive and Reverse Display Questions

11. Display Linked List in Reverse Order

c
void reverseDisplay(Node* head) {

    if (!head) return;

    reverseDisplay(head->next);
    printf("%d ", head->data);
}

Circular Linked List and Loop Questions

12. Convert a Linked List into a Circular Linked List

c
void convertToCircular(Node* head) {

    if (!head) return;

    Node* temp = head;

    while(temp->next)
        temp = temp->next;

    temp->next = head;
}

13. Detect a Loop Using Floyd’s Cycle Detection Algorithm

c
int detectLoop(Node* head) {

    Node *slow = head, *fast = head;

    while(fast && fast->next) {

        slow = slow->next;
        fast = fast->next->next;

        if (slow == fast)
            return 1;
    }

    return 0;
}

Linked List Practice Questions

14. Delete Nodes with Odd Values

c
void deleteOdd(Node** head) {

    while(*head && (*head)->data % 2 != 0) {

        Node* temp = *head;
        *head = (*head)->next;
        free(temp);
    }

    Node* curr = *head;

    while(curr && curr->next) {

        if (curr->next->data % 2 != 0) {

            Node* temp = curr->next;
            curr->next = curr->next->next;
            free(temp);

        } else {
            curr = curr->next;
        }
    }
}

15. Display Alternate Nodes

c
void displayAlternate(Node* head) {

    int toggle = 1;

    while(head) {

        if (toggle)
            printf("%d ", head->data);

        toggle = !toggle;
        head = head->next;
    }

    printf("\n");
}

16. Delete Alternate Nodes

c
void deleteAlternate(Node* head) {

    while(head && head->next) {

        Node* temp = head->next;
        head->next = temp->next;

        free(temp);

        head = head->next;
    }
}

Stack and Queue Representation Using Linked List

17. Print Stack Like Queue

c
void printStackAsQueue(Node* top) {

    reverseDisplay(top);
}

18. Print Queue Like Stack

c
void printQueueAsStack(Node* front) {

    reverseDisplay(front);
}

Double Pointer and Call by Reference Questions

19. Linked List Implementation Using Double Pointers

c
void addNodeDoublePtr(Node** head, int val) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = val;
    newNode->next = NULL;

    if (!*head) {

        *head = newNode;

    } else {

        Node* temp = *head;

        while(temp->next)
            temp = temp->next;

        temp->next = newNode;
    }
}

20. Call by Reference Linked List Insertion

c
void insertByRef(Node** head, int val) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = val;
    newNode->next = *head;

    *head = newNode;
}

Conclusion

These linked list questions cover the most important operations used in data structures and algorithms. By practicing insertion, deletion, searching, splitting, recursion, and circular linked lists, you can build a strong foundation in C programming and improve your problem-solving skills.

These examples are especially useful for:

  • University practical exams

  • Viva preparation

  • Technical interviews

  • Coding practice

  • DSA revision

You can further enhance your learning by implementing doubly linked lists, circular doubly linked lists, stacks, queues, and polynomial representation using linked lists.