Linked List Foundation Visualizer
DSA Linked List Foundation

Linked List Foundation

What is a linked list

Node Structure

A linked list node contains two parts: data (the value) and next (pointer to the next node). The last node's next pointer is NULL.

Data
10
Next
→
Node 1
→
Data
20
Next
→
Node 2
→
Data
30
Next
NULL
Node 3

A linked list is a linear data structure where elements are stored in nodes, each pointing to the next node. Unlike arrays, linked lists don't require contiguous memory, allowing dynamic memory allocation and efficient insertions/deletions at any position.

  • Node Structure: Contains data and a pointer to the next node
  • Head Pointer: Points to the first node in the list
  • Dynamic Memory: Each node is allocated individually during runtime
  • Key Operations:
    • Insert at beginning O(1)
    • Insert at end O(n) or O(1) with tail pointer
    • Delete O(n)
    • Search/Traverse O(n)
  • Real-world Applications:
    • Undo/Redo functionality
    • Navigation in multimedia players
    • Hash table collision handling
    • Polynomial representation
    • Implementation of queues and stacks

Try it

A linked list dynamically stores elements as nodes connected via pointers. Use the visualizer to insert values at the beginning or end, delete nodes, and see how the list structure changes.

linkedlist.svg — Singly Linked List View

Pseudocode & Complexity

pseudocode.txt

1// Linked List Operations
2class Node:
3data
4next = NULL
5
6function insertBeginning(head, value):
7newNode = new Node(value)
8newNode.next = head
9return newNode
10
11function insertEnd(head, value):
12if head == NULL: return insertBeginning(head, value)
13current = head
14while current.next != NULL:
15current = current.next
16current.next = new Node(value)
17function deleteFirst(head):
18if head == NULL: return NULL
19temp = head
20head = head.next
21free(temp)
22return head

complexity.md

OperationTime
Insert at StartO(1)
Insert at EndO(n)
Delete FirstO(1)
Search/TraverseO(n)
SpaceO(n)

C Implementation

linkedlist.c

1#include <stdio.h>
2#include <stdlib.h>
3
4typedef struct Node {
5int data;
6struct Node *next;
7} Node;
8
9Node* createNode(int data) {
10Node* newNode = (Node*)malloc(sizeof(Node));
11newNode->data = data;
12newNode->next = NULL;
13return newNode;
14}
15
16Node* insertBeginning(Node* head, int data) {
17Node* newNode = createNode(data);
18newNode->next = head;
19return newNode;
20}
21
22Node* insertEnd(Node* head, int data) {
23Node* newNode = createNode(data);
24if (head == NULL) return newNode;
25Node* current = head;
26while (current->next != NULL)
27current = current->next;
28current->next = newNode;
29return head;
30}
31
32Node* deleteFirst(Node* head) {
33if (head == NULL) return NULL;
34Node* temp = head;
35head = head->next;
36free(temp);
37return head;
38}
39
40void traverse(Node* head) {
41Node* current = head;
42while (current != NULL) {
43printf("%d → ", current->data);
44current = current->next;
45}
46printf("NULL\n");
47}
48
49int main() {
50Node* head = NULL;
51head = insertBeginning(head, 10);
52head = insertBeginning(head, 5);
53head = insertEnd(head, 20);
54printf("Original list: "); traverse(head);
55head = deleteFirst(head);
56printf("After delete: "); traverse(head);
57return 0;
58}