Bird
Raised Fist0
DSA Cprogramming

Creating a Singly Linked List from Scratch in DSA C - Step-by-Step

Choose your learning style10 modes available
Mental Model
A singly linked list is a chain of nodes where each node points to the next one, forming a line.
Analogy: Imagine a treasure hunt where each clue (node) tells you where to find the next clue, leading you step by step to the treasure.
head -> [node1] -> [node2] -> [node3] -> null
Dry Run Walkthrough
Input: Create a singly linked list with values 1, 2, 3 in order
Goal: Build a linked list where each node holds a value and points to the next node, ending with null
Step 1: Create first node with value 1 and set head to it
head -> [1] -> null
Why: We need a starting point for the list
Step 2: Create second node with value 2 and link first node to it
head -> [1] -> [2] -> null
Why: Link the new node to the list to extend it
Step 3: Create third node with value 3 and link second node to it
head -> [1] -> [2] -> [3] -> null
Why: Continue linking nodes to build the list
Result:
head -> 1 -> 2 -> 3 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Define the node structure
struct Node {
    int data;
    struct Node* next;
};

// Function to create a new node with given data
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data; // store data in node
    newNode->next = NULL; // next pointer is null initially
    return newNode;
}

// Function to print the linked list
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data); // print current node data
        current = current->next; // move to next node
    }
    printf("null\n");
}

int main() {
    struct Node* head = NULL; // start with empty list

    // Step 1: create first node
    head = createNode(1);

    // Step 2: create second node and link
    head->next = createNode(2);

    // Step 3: create third node and link
    head->next->next = createNode(3);

    // Print the final list
    printList(head);

    return 0;
}
newNode->data = data; // store data in node
store the value in the new node
newNode->next = NULL; // next pointer is null initially
initialize next pointer to null since it's a new node
head = createNode(1);
create the first node and set it as head
head->next = createNode(2);
create second node and link it to first
head->next->next = createNode(3);
create third node and link it to second
while (current != NULL) {
traverse the list until the end
current = current->next; // move to next node
advance to next node to continue traversal
OutputSuccess
1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(n) because we create and link n nodes one by one
Space: O(n) because we allocate memory for n nodes
vs Alternative: Compared to arrays, linked lists allow dynamic size without shifting elements, but accessing nodes is slower since we must follow pointers
Edge Cases
empty list (no nodes created)
head remains NULL and printList prints 'null'
DSA C
while (current != NULL) {
single node list
list has one node pointing to null, printed correctly
DSA C
head = createNode(1);
When to Use This Pattern
When you need a flexible list of items where size changes often, and you want to add nodes easily, think of creating a singly linked list from scratch.
Common Mistakes
Mistake: Forgetting to set the next pointer of a new node to NULL
Fix: Always initialize newNode->next = NULL to mark the end of the list
Mistake: Not linking the new node to the previous node
Fix: Assign the previous node's next pointer to the new node to maintain the chain
Summary
Creates a chain of nodes where each node points to the next, forming a singly linked list.
Use when you want a list that can grow dynamically without fixed size.
Remember each node must store data and a pointer to the next node, ending with null.

Practice

(1/5)
1. What is the main purpose of a singly linked list in data structures?
easy
A. To store elements with direct access by index like an array
B. To store elements in a sequence where each element points to the next
C. To store elements in a sorted order automatically
D. To store elements with multiple pointers to previous nodes

Solution

  1. Step 1: Understand singly linked list structure

    A singly linked list stores elements where each node points only to the next node, forming a chain.
  2. Step 2: Compare with other data structures

    Unlike arrays, singly linked lists do not provide direct index access, and unlike doubly linked lists, nodes have only one pointer.
  3. Final Answer:

    To store elements in a sequence where each element points to the next -> Option B
  4. Quick Check:

    Singly linked list = sequence with next pointers [OK]
Hint: Remember: singly linked list nodes have one next pointer only [OK]
Common Mistakes:
  • Thinking singly linked list allows direct index access
  • Confusing singly linked list with doubly linked list
  • Assuming automatic sorting in singly linked list
2. Which of the following is the correct way to define a node structure for a singly linked list in C?
easy
A. typedef struct Node { int data; struct Node* next; } Node;
B. typedef struct Node { int data; Node* next; } Node;
C. struct Node { int data; struct Node next; };
D. struct Node { int data; struct Node* prev; };

Solution

  1. Step 1: Check pointer usage in struct

    The next pointer must be a pointer to struct Node, so use struct Node* next;.
  2. Step 2: Verify typedef correctness

    typedef struct Node { int data; struct Node* next; } Node; correctly uses typedef and pointer syntax for next node.
  3. Final Answer:

    typedef struct Node { int data; struct Node* next; } Node; -> Option A
  4. Quick Check:

    Node struct uses pointer to struct Node for next [OK]
Hint: Use 'struct Node* next;' inside struct for next pointer [OK]
Common Mistakes:
  • Omitting 'struct' keyword before Node* inside struct
  • Using 'Node* next;' without typedef defined yet
  • Using 'struct Node next;' instead of pointer
  • Using 'prev' pointer instead of 'next' for singly linked list
3. What will be the output of the following C code snippet that creates a singly linked list with two nodes and prints their data?
typedef struct Node {
    int data;
    struct Node* next;
} Node;

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 10;
    head->next = malloc(sizeof(Node));
    head->next->data = 20;
    head->next->next = NULL;

    Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    return 0;
}
medium
A. 10 20
B. 20 10
C. 10
D. Compilation error

Solution

  1. Step 1: Trace node creation and data assignment

    First node data is 10, second node data is 20, linked correctly.
  2. Step 2: Trace printing loop

    Loop prints data of head (10), then moves to next node (20), then stops at NULL.
  3. Final Answer:

    10 20 -> Option A
  4. Quick Check:

    Prints nodes in order: 10 then 20 [OK]
Hint: Follow next pointers to print nodes in order [OK]
Common Mistakes:
  • Confusing order of nodes printed
  • Forgetting to set next of last node to NULL
  • Assuming compilation error due to missing includes (not shown)
4. Identify the error in the following code that attempts to create a singly linked list node and assign data:
typedef struct Node {
    int data;
    struct Node* next;
} Node;

int main() {
    Node* head;
    head->data = 5;
    head->next = NULL;
    return 0;
}
medium
A. data should be a pointer, not int
B. next pointer should point to previous node
C. head is not initialized before accessing its members
D. struct Node definition is incorrect

Solution

  1. Step 1: Check pointer initialization

    head is declared but not assigned memory before accessing its members.
  2. Step 2: Understand consequences

    Accessing head->data without allocation causes undefined behavior or crash.
  3. Final Answer:

    head is not initialized before accessing its members -> Option C
  4. Quick Check:

    Pointer must be allocated before use [OK]
Hint: Always allocate memory before using pointer members [OK]
Common Mistakes:
  • Assuming pointer is auto-initialized
  • Confusing next pointer direction
  • Thinking data must be pointer type
5. You want to create a singly linked list from scratch that stores integers 1, 2, 3 in order. Which sequence of operations correctly builds the list?
hard
A. Allocate node1, set data=1, set node1->next=NULL, allocate node2, set data=2, set node2->next=NULL, allocate node3, set data=3, set node3->next=NULL
B. Allocate node1, set data=1, allocate node2, set data=2, allocate node3, set data=3, link node3->next=node2, link node2->next=node1, set node1->next=NULL
C. Allocate node1, set data=1, link node1->next=node2, allocate node2, set data=2, link node2->next=node3, allocate node3, set data=3, set node3->next=NULL
D. Allocate node1, set data=1, allocate node2, set data=2, link node1->next=node2, allocate node3, set data=3, link node2->next=node3, set node3->next=NULL

Solution

  1. Step 1: Allocate nodes and assign data in order

    Nodes must be allocated and data assigned before linking.
  2. Step 2: Link nodes in correct sequence

    Link node1 to node2, node2 to node3, and node3 to NULL to end list.
  3. Final Answer:

    Allocate node1, set data=1, allocate node2, set data=2, link node1->next=node2, allocate node3, set data=3, link node2->next=node3, set node3->next=NULL -> Option D
  4. Quick Check:

    Correct order and linking builds list 1->2->3->NULL [OK]
Hint: Link nodes only after allocating and setting data [OK]
Common Mistakes:
  • Linking nodes before allocation
  • Not setting last node's next to NULL
  • Linking nodes in reverse order
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