</>EmbeddedInterview

Singly Linked List in C – All Operations for Interview Preparation

EasyCoding: Linked Lists, Stacks & Queues

A linked list consists of nodes where each node contains data and a pointer to the next node.

head → 10 → 20 → 30 → NULL

1. Define structure and create a node

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

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

struct Node *head = NULL;

struct Node *createNode(int data)
{
    struct Node *new = malloc(sizeof(struct Node));

    if (new == NULL)
        exit(EXIT_FAILURE);

    new->data = data;
    new->next = NULL;

    return new;
}

2. Insert at beginning

void insertBegin(int data)
{
    struct Node *new = createNode(data);

    new->next = head;
    head = new;
}

Time complexity: O(1)

3. Insert at end

void insertEnd(int data)
{
    struct Node *new = createNode(data);

    if (head == NULL) {
        head = new;
        return;
    }

    struct Node *temp = head;

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

    temp->next = new;
}

Time complexity: O(n)

4. Insert at any position

void insertPos(int data, int pos)
{
    if (pos < 1)
        return;

    if (pos == 1) {
        insertBegin(data);
        return;
    }

    struct Node *temp = head;

    for (int i = 1; i < pos - 1 && temp; i++)
        temp = temp->next;

    if (temp == NULL)
        return;

    struct Node *new = createNode(data);

    new->next = temp->next;
    temp->next = new;
}

Position starts from 1. Time complexity: O(n)

5. Delete from beginning

void deleteBegin()
{
    if (head == NULL)
        return;

    struct Node *temp = head;

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

Time complexity: O(1)

6. Delete from end

void deleteEnd()
{
    if (head == NULL)
        return;

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

    struct Node *temp = head;

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

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

Time complexity: O(n)

7. Delete from any position

void deletePos(int pos)
{
    if (head == NULL || pos < 1)
        return;

    if (pos == 1) {
        deleteBegin();
        return;
    }

    struct Node *temp = head;

    for (int i = 1; i < pos - 1 && temp; i++)
        temp = temp->next;

    if (temp == NULL || temp->next == NULL)
        return;

    struct Node *del = temp->next;

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

Time complexity: O(n)

8. Delete by value

void deleteValue(int data)
{
    struct Node *temp = head;
    struct Node *prev = NULL;

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

    if (temp == NULL)
        return;

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

    free(temp);
}

Deletes the first occurrence of the given value.

9. Print linked list

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

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

    printf("NULL\n");
}

10. Search an element

int search(int data)
{
    struct Node *temp = head;
    int pos = 1;

    while (temp) {
        if (temp->data == data)
            return pos;

        temp = temp->next;
        pos++;
    }

    return -1;
}

Returns its position or -1 if not found.

11. Count number of nodes

int countNodes()
{
    int count = 0;
    struct Node *temp = head;

    while (temp) {
        count++;
        temp = temp->next;
    }

    return count;
}

12. Reverse a linked list (very important)

void reverse()
{
    struct Node *prev = NULL;
    struct Node *curr = head;
    struct Node *next;

    while (curr) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }

    head = prev;
}

Time: O(n) | Space: O(1)

Original: 10 → 20 → 30 → NULL. Reverse each link in turn. Result: 30 → 20 → 10 → NULL.

13. Find middle node

struct Node *findMiddle()
{
    struct Node *slow = head;
    struct Node *fast = head;

    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }

    return slow;
}

Using slow and fast pointers.

  • Slow moves one node at a time.
  • Fast moves two nodes at a time.
  • When fast reaches the end, slow is at the middle.

For even-length lists, this returns the second middle node.

14. Detect loop (Floyd's algorithm)

int detectLoop()
{
    struct Node *slow = head;
    struct Node *fast = head;

    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;

        if (slow == fast)
            return 1;
    }

    return 0;
}

Time: O(n) | Space: O(1)

15. Find Nth node from end

struct Node *nthFromEnd(int n)
{
    struct Node *fast = head;
    struct Node *slow = head;

    if (n <= 0)
        return NULL;

    while (n--) {
        if (!fast)
            return NULL;

        fast = fast->next;
    }

    while (fast) {
        slow = slow->next;
        fast = fast->next;
    }

    return slow;
}

16. Remove duplicates from a sorted linked list

void removeDuplicates()
{
    struct Node *temp = head;

    while (temp && temp->next) {
        if (temp->data == temp->next->data) {
            struct Node *del = temp->next;
            temp->next = del->next;
            free(del);
        }
        else
            temp = temp->next;
    }
}

17. Delete entire linked list

void freeList()
{
    while (head)
        deleteBegin();
}

18. Main function to test operations

int main()
{
    insertBegin(10);
    insertBegin(5);
    insertEnd(20);
    insertEnd(30);
    insertPos(15, 3);

    printList();

    deleteBegin();
    deleteEnd();
    deletePos(2);

    printList();

    reverse();
    printList();

    printf("Count = %d\n", countNodes());

    freeList();

    return 0;
}

Combine the above functions into a single C file.

Output:

5 -> 10 -> 15 -> 20 -> 30 -> NULL
10 -> 20 -> NULL
20 -> 10 -> NULL
Count = 2

Time complexity cheat sheet

Operation Time complexity
Insert at beginning O(1)
Insert at end O(n)
Insert at position O(n)
Delete beginning O(1)
Delete end O(n)
Delete at position O(n)
Delete by value O(n)
Search O(n)
Traversal O(n)
Reverse O(n)
Find middle O(n)
Detect loop O(n)
Nth node from end O(n)
Remove duplicates (sorted) O(n)

Most important linked list questions for embedded interviews

For Microsoft, Arm, Cisco and other embedded software interviews, prioritize these:

  1. Reverse a linked list.
  2. Detect a loop in a linked list.
  3. Detect the starting point of a loop.
  4. Remove a loop.
  5. Find the middle node.
  6. Find the Nth node from the end.
  7. Merge two sorted linked lists.
  8. Find the intersection of two linked lists.
  9. Reverse a linked list in groups of K.
  10. Check whether a linked list is a palindrome.
  11. Implement a doubly linked list.
  12. Implement a circular linked list.
  13. Implement a linked list without malloc() using a static memory pool.
  14. Implement an LRU cache using a doubly linked list and hash table.

Embedded-specific tip: The above implementation uses a global head pointer to keep the code simple for interviews. In production code, prefer passing struct Node **head to functions that modify the head. For deterministic embedded systems, also understand static memory pool allocation as an alternative to malloc().

Note for subsequent questions: The remaining examples in this topic use a separate type, typedef struct node { int val; struct node *next; } node_t;, rather than struct Node used above.