codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C linked lists (DSA)

C linked lists (DSA)

Nodes linked by pointers.

Each node stores data and a pointer to the next. Insert at the head in constant time.

Example code

#include <stdio.h>
#include <stdlib.h>
struct N { int v; struct N *next; };
int main() {
  struct N *h = NULL;
  for (int i = 1; i <= 3; i++) {
    struct N *n = malloc(sizeof *n);
    n->v = i; n->next = h; h = n;
  }
  for (struct N *p = h; p; p = p->next) printf("%d ", p->v);
  printf("\n");
  return 0;
}

Run this code in the online editor

← C stacks and queues (DSA)C Big O (DSA) →