DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on • Edited on

πŸ”— Linked List: 0 100% Mastery Roadmap

🟒 1. WHAT IS A LINKED LIST?

A Linked List is a linear data structure in which elements are stored in the form of nodes, and each node is connected to the next node using a pointer (reference).

Unlike arrays, elements in a linked list are not stored in contiguous memory locations. Instead, they are stored at different (non-contiguous) memory locations and are connected using pointers (or references).

πŸ”Ή Basic Concept (Easy Words)

  • A linked list is made up of nodes (small blocks of data)

  • Each node contains:

    • Data β†’ stores the value
    • Next (Pointer) β†’ stores the address of the next node
  • Nodes are connected using pointers, forming a chain-like structure

  • Nodes are stored in different (non-contiguous) memory locations


πŸ”Ή Important Terms

πŸ”Έ HEAD

  • Pointer to the first node of the list
  • Entry point to access the linked list

πŸ”Έ NODE

  • Basic unit of a linked list
  • Contains:
    • Data
    • Pointer (next address)

πŸ”Έ POINTER / REFERENCE

  • Stores the address of another node
  • Used to connect nodes together

πŸ”Έ NULL

  • Indicates end of the list
  • Means no next node exists

πŸ”Έ TRAVERSAL

  • Process of visiting each node
  • Starts from head and moves till NULL

πŸ”Έ LENGTH

  • Total number of nodes in the list
  • Found by traversing the entire list

πŸ”Έ NEXT

  • Pointer inside a node
  • Stores address of the next node

πŸ”Έ DYNAMIC MEMORY

  • Memory is allocated as needed
  • No need for fixed size

πŸ”Έ NON-CONTIGUOUS MEMORY

  • Nodes are stored in random memory locations
  • Not stored in sequence like arrays

πŸ“Œ What is a Node?

πŸ‘‰ A node is like a Container which contains data and a reference to the next node in a linked list.

Think of a node as a basic building block used to create a linked list.


πŸ”Ή Simple Real-Life Meaning

Imagine a train:

  • Each coach (bogie) = a node
  • Inside each coach = data (passengers)
  • Connection between coaches = pointer (link)

So, the train works only because all coaches are connected.

πŸ‘‰ Similarly, a linked list works because all nodes are connected.

πŸ”Ή What Does a Node Contain?

A node has two parts:

1. πŸ“¦ Data (Value)

This is the actual information stored in the node.

Examples:

  • 10
  • 50
  • "A"
  • 100

2. πŸ”— Link (Pointer)

This stores the address of the next node.

It tells:
πŸ‘‰ β€œWhere is the next node?”


πŸ”Ή Structure of a Node

[ Data | Next ]
Enter fullscreen mode Exit fullscreen mode
  • Data β†’ value stored in the node
  • Next β†’ address of the next node

πŸ”Ή Example of Linked Nodes

10 β†’ 20 β†’ 30 β†’ 40 β†’ NULL
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Each value is stored in a separate node
  • Each node is connected using a pointer
  • The last node points to NULL (end of list)

__

🎯 3. Core Idea (Very Important)

πŸ‘‰ To create a node in a singly linked list, we create an object that contains two properties: value and next.

  • Instead of creating objects manually, we use a constructor function or class. Using the new keyword, JavaScript creates a new object, assigns value and next, and returns the node.



{
  value: 5,
  next: null
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This object has:

  • data (value)
  • pointer (next)

❌ 4. Problem with Manual Creation

If we create nodes manually:

let node1 = { value: 5, next: null };
let node2 = { value: 10, next: null };
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Problems:

  • Repetitive ❌
  • Time-consuming ❌
  • Not scalable ❌

βœ… 5. Solution β†’ Use Function / Class

πŸ‘‰ Instead of creating objects manually, we use a constructor function (or class)

So:

  • We don’t create objects ourselves
  • We just call a function
  • It creates the object for us automatically βœ…

πŸ—οΈ 6. Final Code (Constructor Function)

function Node(val) {
  this.value = val;   // store data
  this.next = null;   // store reference to next node
}
Enter fullscreen mode Exit fullscreen mode

🧠 7. What is Happening Internally?

When you write:

let node1 = new Node(5);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ JavaScript performs these steps:

πŸ”₯ Step-by-step (Dry Run)

  1. Create empty object:
{}
Enter fullscreen mode Exit fullscreen mode
  1. this points to that object:

```javascript id="step2"
this = {}




3. Assign values:



```javascript id="step3"
this.value = 5
this.next = null
Enter fullscreen mode Exit fullscreen mode
  1. Return object:

```javascript id="step4"
{
value: 5,
next: null
}




πŸ‘‰ So finally:



```javascript id="final-object"
node1 = {
  value: 5,
  next: null
}
Enter fullscreen mode Exit fullscreen mode

πŸ” 8. Creating Multiple Nodes (Key Idea)

πŸ‘‰ You don’t create objects manually again and again

Instead:

let node1 = new Node(5);
let node2 = new Node(10);
let node3 = new Node(20);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Every time you call:

new Node(value)
Enter fullscreen mode Exit fullscreen mode

➑️ A new object is created automatically


πŸ”— 9. Connecting Nodes

node1.next = node2;
node2.next = node3;
Enter fullscreen mode Exit fullscreen mode

πŸ–ΌοΈ 10. Visualization

[5 | next] β†’ [10 | next] β†’ [20 | null]
Enter fullscreen mode Exit fullscreen mode

🧠 11. What is this?

πŸ‘‰ this = current object being created

It allows us to:

  • Put data inside object
  • Build structure dynamically

πŸš€ 12. Why new Keyword is MUST

πŸ‘‰ new does everything automatically:

  1. Creates object
  2. Links this
  3. Executes function
  4. Returns object

❌ Without new

let node1 = Node(5);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Wrong because:

  • No object creation ❌
  • this breaks ❌

🧠 13. Memory Concept (Interview Level)

πŸ‘‰ next does NOT store full node
πŸ‘‰ It stores reference (address)

Example:

node1 β†’ { value: 5, next: address of node2 }
node2 β†’ { value: 10, next: null }
Enter fullscreen mode Exit fullscreen mode

⚠️ 14. Common Mistake

this.value = val;
this.value = null; // ❌ wrong
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Always:

this.next = null;
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 15. Class Version (Modern JavaScript)

class Node {
  constructor(val) {
    this.value = val;
    this.next = null;
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Same working, cleaner syntax βœ…

πŸ”Ή Easy Way to Understand

Think of a treasure hunt game:

  • Each clue contains:

    • πŸ† Reward (data)
    • πŸ“ Next location (pointer)

You keep moving from one clue to the next.

πŸ‘‰ This is exactly how nodes work.

πŸ”Ή Why Linked List is Important?

A linked list is important because it provides a dynamic and flexible way of storing data.

It is used when:

  • Size of data is unknown in advance
  • Frequent insertion and deletion is required
  • Memory should be used efficiently at runtime

πŸ”Ή Advantages of Linked List

  • πŸ“Œ Dynamic size (can grow and shrink at runtime)
  • πŸ“Œ No need for continuous (contiguous) memory
  • πŸ“Œ Easy insertion and deletion (no shifting required)
  • πŸ“Œ Memory is allocated only when needed
  • πŸ“Œ Useful for implementing Stack, Queue, Graph, and Hash collision handling

πŸ”₯ Linked List vs Array (Very Important for Exams & Interviews)


πŸ”Ή 1. Basic Structure Difference

Array

  • Stores elements in continuous (contiguous) memory
  • Uses index-based access
Index:  0   1   2   3
Array:  3   4   5   1
Enter fullscreen mode Exit fullscreen mode

Linked List

  • Stores elements in non-contiguous memory
  • Uses nodes (Data + Pointer)
3 β†’ 4 β†’ 5 β†’ 1 β†’ NULL
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 2. Node Structure (Core Concept)

A node contains:

[ Data | Pointer ]
Enter fullscreen mode Exit fullscreen mode
  • Data β†’ actual value
  • Pointer β†’ address of next node

πŸ‘‰ In doubly linked list:

[ Prev | Data | Next ]
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 3. Memory Difference

Array (Contiguous Memory)

| 3 | 4 | 5 | 1 |
Enter fullscreen mode Exit fullscreen mode
  • Memory is continuous
  • No extra pointer memory

Linked List (Non-Contiguous Memory)


[3 | β€’ ] β†’ [4 | β€’ ] β†’ [5 | β€’ ] β†’ [1 | NULL]
Enter fullscreen mode Exit fullscreen mode
  • Nodes are stored randomly in memory
  • Extra memory used for pointers
  • Doubly linked list uses more memory than singly linked list

πŸ”Ή 4. Example (3, 4, 5, 1)

Array

Index:  0   1   2   3
Value:  3   4   5   1
Enter fullscreen mode Exit fullscreen mode

Linked List

3 β†’ 4 β†’ 5 β†’ 1 β†’ NULL
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 5. Accessing Elements

Array β†’ Fast Access

arr[2] = 5 β†’ O(1)
Enter fullscreen mode Exit fullscreen mode

βœ” Direct index access
βœ” Very fast


Linked List β†’ Slow Access

To access an element:

3 β†’ 4 β†’ 5 β†’ 1
Enter fullscreen mode Exit fullscreen mode

To reach "5":

  • Start from head
  • Traverse node by node

❌ No direct access
βœ” Time Complexity: O(n)


πŸ”Ή 6. Insertion & Deletion

Linked List β†’ Easy

  • No shifting required
  • Only pointer changes

Example:

3 β†’ 4 β†’ 5 β†’ 1
Insert 10 between 4 and 5
Enter fullscreen mode Exit fullscreen mode

βœ” Efficient (O(1) if position known)


Array β†’ Difficult

  • Requires shifting elements
3 4 5 1
Insert 10 at index 1 β†’ shift elements
Enter fullscreen mode Exit fullscreen mode

❌ Time Complexity: O(n)


πŸ”Ή 7. Memory Usage

Structure Memory
Array Efficient (no pointers)
Singly Linked List Extra memory (1 pointer/node)
Doubly Linked List More memory (2 pointers/node)

πŸ”Ή 8. Time Complexity Comparison

Operation Array Linked List
Access O(1) O(n)
Search O(n) O(n)
Insert (beginning) O(n) O(1)
Insert (middle) O(n) O(1)*
Delete O(n) O(1)*
  • if node reference is known

πŸ”Ή 9. Key Differences (Quick Revision)

Array:

  • Fast access
  • Fixed size (or costly resizing)
  • Contiguous memory
  • Poor insertion/deletion

Linked List:

  • Slow access
  • Dynamic size
  • Non-contiguous memory
  • Easy insertion/deletion

πŸ”Ή 10. Real-Life Use Cases

Array:

  • Image processing
  • Index-based systems
  • Static data storage

Linked List:

  • Music playlist
  • Browser history
  • Undo/Redo system
  • Memory management
  • Graph adjacency list

🟑 HOW LINKED LIST STORES DATA IN MEMORY

πŸ”Ή Basic Idea

  • Nodes are stored in random (non-contiguous) memory locations
  • Each node contains:

    • Data
    • Pointer (address) to the next node
  • Connection is logical (via pointers), not physical


πŸ“Œ Structure of a Node

[ Data | Next Address ]
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Memory Representation

Memory Addresses:   100    500    200    800

Nodes:
[10 | 500] β†’ [20 | 200] β†’ [30 | 800] β†’ [40 | NULL]
Enter fullscreen mode Exit fullscreen mode
  • 100 stores data 10 and points to 500
  • 500 stores data 20 and points to 200
  • 200 stores data 30 and points to 800
  • 800 stores data 40 and points to NULL (end of list)

πŸ”Ή Important Points

  • The first node is accessed using a pointer called head
  • The last node points to NULL
  • Nodes are not stored sequentially in memory
  • Order is maintained using addresses (links)

πŸ”Ή Advantages

  • Dynamic size (can grow/shrink easily)
  • Efficient insertion/deletion (no shifting required)
  • Memory is used as needed

πŸ”Ή Disadvantages

  • Extra memory needed for pointers
  • No direct access (must traverse from head)
  • Slower access compared to arrays

⚑ Key Comparison with Array

Feature Array Linked List
Memory Contiguous Non-contiguous
Size Fixed Dynamic
Access Direct (fast) Sequential (slow)
Insertion Costly (shift) Easy (pointer change)

🟑 TYPES OF LINKED LIST (DETAILED)

Linked List is a linear data structure where elements (nodes) are connected using pointers.

πŸ”Ή 1. SINGLY LINKED LIST (SLL)

πŸ“Œ Definition

  • Each node contains:
    • Data
    • One pointer (Next)
  • Points only to the next node

πŸ“Œ Structure


[Data | Next] β†’ [Data | Next] β†’ [Data | NULL]

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Key Points

  • Traversal is only forward
  • Last node points to NULL
  • Simplest type of linked list

βœ… Advantages

  • Less memory (only one pointer)
  • Easy to implement

❌ Disadvantages

  • Cannot move backward
  • Traversal is slow (O(n))

πŸ’‘ Use Cases

  • Stacks
  • Basic dynamic memory structures

πŸ”Ή 2. DOUBLY LINKED LIST (DLL)

πŸ“Œ Definition

  • Each node contains:
    • Previous pointer
    • Data
    • Next pointer

πŸ“Œ Structure


NULL ← [Prev | Data | Next] ⇄ [Prev | Data | Next] β†’ NULL

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Key Points

  • Traversal in both directions
  • Extra memory needed for prev

βœ… Advantages

  • Easy backward traversal
  • Easier deletion (no need to track previous node)

❌ Disadvantages

  • More memory usage
  • More complex

πŸ’‘ Use Cases

  • Navigation systems (back/forward)
  • Undo/Redo operations

πŸ”Ή 3. CIRCULAR LINKED LIST (CLL)

πŸ“Œ Definition

  • Last node connects back to first node
  • No node points to NULL

πŸ“Œ Structure


[Data | Next] β†’ [Data | Next] β†’ [Data | Next]
↑__________________________________↓

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Key Points

  • Forms a loop
  • Can start traversal from any node

βœ… Advantages

  • No NULL pointer issues
  • Efficient for cyclic processes

❌ Disadvantages

  • Infinite loop risk
  • Harder to debug

πŸ’‘ Use Cases

  • Round-robin scheduling
  • Multiplayer games (turn rotation)

πŸ”Ή 4. CIRCULAR DOUBLY LINKED LIST (CDLL)

πŸ“Œ Definition

  • Combination of DLL + Circular
  • Each node has:
    • Prev pointer
    • Next pointer
  • First and last nodes are connected

πŸ“Œ Structure


↔ [Prev | Data | Next] ⇄ [Prev | Data | Next] ↔
↑____________________________________________↓

Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Key Points

  • Traversal in both directions
  • No NULL pointer

βœ… Advantages

  • Full flexibility (forward + backward)
  • Efficient for continuous navigation

❌ Disadvantages

  • Most complex
  • Highest memory usage

πŸ’‘ Use Cases

  • Advanced navigation systems
  • Music/playlist looping

🧠 IMPORTANT INTERVIEW POINTS

πŸ”Έ Difference Between Types

Feature Singly LL Doubly LL Circular LL Circular Doubly LL
Pointers 1 2 1 2
Direction One-way Two-way One-way Two-way
NULL Present Yes Yes No No
Memory Usage Low Medium Low High
Complexity Easy Medium Medium Hard

πŸ”Έ Time Complexity (Common)

Operation SLL DLL CLL CDLL
Traversal O(n) O(n) O(n) O(n)
Insertion (head) O(1) O(1) O(1) O(1)
Deletion O(n) O(1)* O(n) O(1)*

*O(1) deletion in DLL/CDLL if node is known


πŸ”Έ When to Use Which?

  • Use Singly LL β†’ when memory is limited
  • Use Doubly LL β†’ when backward traversal needed
  • Use Circular LL β†’ when process is cyclic
  • Use Circular Doubly LL β†’ when full flexibility needed

🟣 7. LINKED LIST VS ARRAY (DEEP)

Feature Array Linked List
Memory Contiguous Non-contiguous
Size Fixed / Resizable Fully dynamic
Access O(1) O(n)
Insert/Delete O(n) O(1) (if pointer known)
Cache Friendly Yes No
Memory Overhead Low High (pointers)

🟣 8. TIME COMPLEXITY CHEAT SHEET

Operation Time
Access O(n)
Search O(n)
Insert at head O(1)
Insert at tail O(1)*
Delete O(1)*
  • if pointer available

🟠 9. CORE OPERATIONS (MUST MASTER)


πŸ“Œ Traversal

  • Visit each node one by one

πŸ“Œ Insertion

  • At beginning
  • At end
  • At position
  • After node
  • Before node

πŸ“Œ Deletion

  • From beginning
  • From end
  • By value
  • By position

πŸ“Œ Update

  • Change node value

πŸ“Œ Search

  • Linear search

🟠 10. EDGE CASES (VERY IMPORTANT)

  • Empty list
  • Single node list
  • Two nodes
  • Deleting head
  • Deleting tail
  • Loop present
  • Duplicate values
  • Large inputs

⚑ When to Use Array

βœ… Use Array When:

  1. Fast Access is Required (O(1))
    • You need to directly jump to any element
    • Example: arr[4] instantly gives value

🏒 Analogy:

You know the exact flat number β†’ go directly there


  1. Data Size is Fixed or Known
    • Size doesn’t change frequently

Example:

  • Marks of 100 students
  • Days in a week

  1. Frequent Read Operations
    • More reading, less inserting/deleting

  1. Better Cache Performance
    • Stored in continuous memory β†’ faster in real systems

❌ Avoid Array When:

  • Frequent insertions/deletions in middle
  • Size changes dynamically

πŸ”— When to Use Linked List

βœ… Use Linked List When:

  1. Frequent Insertions/Deletions
    • No shifting needed
    • Just update pointers

🏠 Analogy:

Insert a new house β†’ just change address slips


  1. Dynamic Size
    • Can grow/shrink anytime

  1. Memory is Fragmented
    • Doesn’t require continuous space

  1. Implementing Advanced Structures
    • Stacks
    • Queues
    • Graphs

❌ Avoid Linked List When:

  • You need fast random access
  • Extra memory (pointer storage) is a concern

//// sdfsegfs

πŸ”΄ 11. IMPORTANT PATTERNS


🧠 1. Two Pointer Technique

  • Slow & Fast pointer

Used in:

  • Cycle detection
  • Middle node

🧠 2. Reversal Pattern

  • Iterative
  • Recursive

🧠 3. Dummy Node Technique

  • Simplifies insertion/deletion

🧠 4. Sliding Pointer

  • Used in partition problems

πŸ”΄ 12. MUST-DO PROBLEMS (INTERVIEW CORE)


Easy

  • Reverse Linked List
  • Find middle
  • Remove duplicates

Medium

  • Detect cycle (Floyd)
  • Merge two sorted lists
  • Remove nth from end
  • Intersection of two lists

Hard

  • LRU Cache πŸ”₯
  • Clone list with random pointer
  • Reverse in k-groups
  • Flatten linked list

πŸ”₯ 13. ADVANCED CONCEPTS


πŸ“Œ Floyd Cycle Detection

  • Fast moves 2 steps
  • Slow moves 1 step

πŸ“Œ Random Pointer Linked List

  • Each node points randomly

πŸ“Œ Skip List

  • Multi-level linked list

πŸ“Œ XOR Linked List (Rare)

  • Memory optimized

πŸ”₯ 14. DOUBLY LINKED LIST (DETAIL)

Node:

[Prev | Data | Next]
Enter fullscreen mode Exit fullscreen mode

Pros:

  • Backward traversal

Cons:

  • Extra memory

πŸ”₯ 15. CIRCULAR LINKED LIST (DETAIL)

  • No NULL
  • Last connects to first

Used in:

  • Round-robin scheduling

🧠 16. MEMORY UNDERSTANDING

  • Each node allocated dynamically
  • Uses heap memory
  • Pointer stores address

🧠 17. COMMON MISTAKES

❌ Forgetting NULL check
❌ Losing head pointer
❌ Infinite loop in circular list
❌ Wrong pointer update order


βš”οΈ 18. INTERVIEW STRATEGY

  1. Draw diagram first ✍️
  2. Handle edge cases
  3. Use dummy node
  4. Dry run example

πŸš€ 19. REAL WORLD USE CASES

  • Music playlist
  • Browser history
  • Undo/Redo
  • Navigation systems

πŸ’° β‚Ή5 CRORE ANALOGY (Linked List Explained)

🏠 Concept Mapping

  • House = Node
  • Money (β‚Ή) = Data
  • Address Slip = Pointer (next)

πŸ”— Structure


House1 β†’ House2 β†’ House3 β†’ House4 β†’ House5 β†’ NULL

Enter fullscreen mode Exit fullscreen mode

🧱 Inside Each House (Node)

Each house contains:

  1. πŸ’° Money (Data)
  2. πŸ“„ Address slip pointing to the next house (Pointer)

🏠 House Breakdown

  • House1

    • Money: β‚Ή1 crore
    • Next: House2
  • House2

    • Money: β‚Ή1 crore
    • Next: House3
  • House3

    • Money: β‚Ή1 crore
    • Next: House4
  • House4

    • Money: β‚Ή1 crore
    • Next: House5
  • House5

    • Money: β‚Ή1 crore
    • Next: NULL (end of list)

🧠 Key Idea: Sequential Access

You cannot jump directly to a house.

To reach House5:


House1 β†’ House2 β†’ House3 β†’ House4 β†’ House5

Enter fullscreen mode Exit fullscreen mode

➑️ You must follow the chain step-by-step.


🧾 Programming Mapping

Real World Programming
House Node
Money Data
Address Slip Pointer (next)
First House Head
Last House Tail (next = NULL)

πŸ’‘ Advantages

1. Dynamic Size

You can easily add more houses:


House5 β†’ House6 β†’ NULL

Enter fullscreen mode Exit fullscreen mode

2. Easy Insertion

Insert a new house between House2 and House3:


House2 β†’ NewHouse β†’ House3

Enter fullscreen mode Exit fullscreen mode

➑️ Only pointers change, no shifting needed.


⚠️ Limitation

  • Slow access
  • To reach House5, you must pass through all previous houses

🎯 Final Intuition

Think of it as a treasure trail:

  • Each house gives you money πŸ’°
  • And tells you where to go next πŸ“
  • If you lose the address slip, the chain breaks ❌

Top comments (0)