---
title: "Algorithm to create a single linked list"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:create-single-linked-list"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/create-single-linked-list
site: BCT Engineering
---

# Algorithm to create a single linked list

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2072 Asoj, 6 marks
- Video: [Creating a Single Linked List (Part 1)](https://www.youtube.com/watch?v=nxtDe6Gq4t4) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/create-single-linked-list
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

Write an algorithm that creates a single linked list.

## Short answer

Read values into new nodes linked at the list end. Initialize start and last as NULL. For each value, allocate a node, set its data and next=NULL. If the list is empty, set start=last=node; else link last->next=node and last=node. Builds list in O(n) time.

## Answer

Creating a singly linked list means reading values repeatedly, making a node for each one and attaching it at the end. We keep `start` (first node) and `last` (current last node).

#### Algorithm CREATE
1. Set `start = NULL`, `last = NULL`.
2. Repeat until the user stops (e.g. enters -1):
   1. Read item `x`.
   2. Allocate a new node `p`; if allocation fails print "no memory" and stop.
   3. Set `p->info = x` and `p->next = NULL`.
   4. If `start == NULL`: `start = p` (first node); else `last->next = p`.
   5. Set `last = p`.
3. Return `start`.

```text
Input 10, 20, 30
Step1: start -> [10|NULL]                 last = 10
Step2: start -> [10|*]->[20|NULL]         last = 20
Step3: start -> [10|*]->[20|*]->[30|NULL] last = 30
```

#### C implementation
```c
#include <stdio.h>
#include <stdlib.h>

struct node { int info; struct node *next; };

struct node *create(void) {
    struct node *start = NULL, *last = NULL, *p;
    int x;
    printf("Enter numbers (-1 to stop): ");
    while (scanf("%d", &x) == 1 && x != -1) {
        p = (struct node *)malloc(sizeof(struct node));
        if (p == NULL) { printf("No memory\n"); break; }
        p->info = x;
        p->next = NULL;
        if (start == NULL) start = p;     /* first node */
        else last->next = p;              /* attach at end */
        last = p;
    }
    return start;
}

void display(struct node *start) {
    for (struct node *p = start; p != NULL; p = p->next)
        printf("%d -> ", p->info);
    printf("NULL\n");
}

int main(void) {
    struct node *head = create();
    display(head);
    return 0;
}
```
Keeping the `last` pointer makes each insertion O(1), so creating n nodes costs O(n).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When creating a list, what is done for the first node?
   A. last->next is set to the node
   B. start is set to NULL
   C. start is set to the new node
   D. The node is freed
   Answer: C. If start is NULL there is no last node, so the new node becomes start. (Key point: First node)

2. For later nodes, how is the new node attached?
   A. p->next = start
   B. start->next = p
   C. last = p only
   D. last->next = p, then last = p
   Answer: D. The new node is linked after the current last node, and last then advances. (Key point: Attaching)

3. What advantage does keeping the last pointer give?
   A. Each insertion at the end is O(1), so creating n nodes costs O(n)
   B. Random access to nodes
   C. No need for malloc
   D. Automatic sorting
   Answer: A. Without last, each insertion would need traversal from start, giving O(n^2) total. (Key point: Role of last)

## Related in Data Structure and Algorithm

- [Polynomial addition using linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apolynomial-addition-linked-list) — Data Structure and Algorithm, chapter 4, asked 3×
- [Types of linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Atypes-of-linked-list) — Data Structure and Algorithm, chapter 4, asked 2×
- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Doubly vs singly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-vs-singly-linked) — Data Structure and Algorithm, chapter 4, asked 2×
- [Stack and queue using linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-queue-singly-linked-list) — Data Structure and Algorithm, chapter 4, asked 2×
- [ADT of linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aadt-linked-list-definition) — Data Structure and Algorithm, chapter 1
