Introduction, Need, Node Structure, Creating a Linked List & Traversal
When learning Data Structures, arrays are usually the first data structure introduced because they are simple and easy to understand. However, arrays have several limitations, especially when we need to insert or delete elements frequently.
To overcome these limitations, computer scientists developed a dynamic data structure called the Linked List.
A Linked List is one of the most important topics in Data Structures and Algorithms (DSA). It is frequently asked in university examinations, coding interviews, and competitive programming.
In this comprehensive guide (Part 1), you’ll learn:
- π What is a Linked List?
- π Why do we need a Linked List?
- π Limitations of Arrays
- π Types of Linked Lists
- π Memory Representation
- π What is a Node?
- π Creating a Singly Linked List in Java
- π Traversing a Linked List
- π Java Programs with Complete Explanation
Let’s begin!
π Table of Contents
- What is a Data Structure?
- What is a Linked List?
- Why Do We Need a Linked List?
- Limitations of Arrays
- Advantages of Linked Lists
- Types of Linked Lists
- What is a Singly Linked List?
- Components of a Node
- Memory Representation
- Creating a Node in Java
- Creating a Singly Linked List
- Traversing a Linked List
- Java Programs
- Time Complexity
- Advantages & Disadvantages
- Frequently Asked Questions
- Conclusion
π» What is a Data Structure?
A Data Structure is a way of organizing and storing data so that it can be accessed and modified efficiently.
Some common data structures are:
- π¦ Arrays
- π Linked Lists
- π Stacks
- πͺ Queues
- π³ Trees
- πΈ Graphs
- π Hash Tables
Each data structure is designed to solve different types of problems.
π What is a Linked List?
A Linked List is a linear data structure in which elements are stored as individual nodes.
Unlike arrays, the nodes are not stored in contiguous memory locations.
Instead, every node stores:
- The actual data.
- The address (reference) of the next node.
Think of it as a chain where each link points to the next link.
[Data | Next] β [Data | Next] β [Data | Next] β NULL
The last node points to NULL, indicating the end of the list.
π€ Why Do We Need a Linked List?
Suppose we have an array:
10 20 30 40 50
Now we want to insert 25 after 20.
The new array becomes:
10 20 25 30 40 50
To make space for 25, we need to shift:
- 30
- 40
- 50
This shifting becomes expensive when the array is large.
Similarly, deleting an element also requires shifting.
Linked Lists solve this problem because insertion and deletion only involve changing references.
β Limitations of Arrays
Although arrays are fast, they have several disadvantages.
1οΈβ£ Fixed Size
Once an array is created, its size cannot be changed.
int arr[] = new int[5];
If you later need 10 elements, you must create another array.
2οΈβ£ Expensive Insertions
Suppose the array is:
10 20 30 40 50
Insert 25
10 20 25 30 40 50
Many elements need to be shifted.
Time Complexity:
[
O(n)
]
3οΈβ£ Expensive Deletions
Delete 20
10 30 40 50
Again, all remaining elements shift.
4οΈβ£ Memory Wastage
If the array size is larger than required,
unused memory is wasted.
β Advantages of Linked Lists
Linked Lists overcome many limitations of arrays.
β Dynamic size
β Easy insertion
β Easy deletion
β No shifting of elements
β Better memory utilization
π Types of Linked Lists
There are mainly four types of linked lists.
1οΈβ£ Singly Linked List
10 β 20 β 30 β NULL
Each node points only to the next node.
2οΈβ£ Doubly Linked List
NULL β 10 β 20 β 30 β NULL
Each node stores both previous and next references.
3οΈβ£ Circular Singly Linked List
10 β 20 β 30
β β
βββββββββββββ
The last node points back to the first node.
4οΈβ£ Circular Doubly Linked List
Each node stores:
- Previous reference
- Next reference
The first and last nodes are connected in both directions.
π What is a Singly Linked List?
A Singly Linked List is the simplest type of linked list.
Each node contains:
- π¦ Data
- π Reference to the next node
Example
10 β 20 β 30 β 40 β NULL
Here,
- 10 points to 20
- 20 points to 30
- 30 points to 40
- 40 points to NULL
π§© Components of a Node
Every node contains two parts.
π¦ Data
Stores the actual information.
Example
10
π Next
Stores the address of the next node.
10 | Next
Complete Node
+------+------+
| Data | Next |
+------+------+
π§ Memory Representation
Suppose we have
10 β 20 β 30
Memory may look like this.
Address Data Next
100 10 250
250 20 400
400 30 NULL
Notice something interesting.
The addresses
100
250
400
are completely random.
Unlike arrays,
Linked List nodes do not occupy contiguous memory.
π― What is Head?
The Head is a special reference that stores the address of the first node.
Head
β
10 β 20 β 30 β NULL
Without Head,
we cannot access the linked list.
π Creating a Node in Java
In Java,
every node is represented using a class.
class Node {
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
Explanation
int data;
Stores the value.
Node next;
Stores the reference of the next node.
Node(int data)
Constructor.
Whenever a new node is created,
it automatically stores the data.
this.next = null;
Initially,
there is no next node.
π» Program 1 β Creating a Single Node
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
public class LinkedListDemo{
public static void main(String args[]){
Node head = new Node(10);
System.out.println(head.data);
}
}
Output
10
π» Program 2 β Creating Three Nodes
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
public class LinkedListDemo{
public static void main(String args[]){
Node first = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
first.next = second;
second.next = third;
System.out.println(first.data);
System.out.println(first.next.data);
System.out.println(first.next.next.data);
}
}
Output
10
20
30
π Visual Representation
first
β
+------+------+
| 10 | ββββΌβββββΊ
+------+------+
|
βΌ
+------+------+
| 20 | ββββΌβββββΊ
+------+------+
|
βΌ
+------+------+
| 30 | NULL |
+------+------+
πΆ Traversing a Linked List
Traversal means visiting every node exactly once.
The idea is simple.
Start from Head.
Move to the next node.
Repeat until NULL.
Algorithm
Step 1
Start from Head
β
Step 2
Print Data
β
Step 3
Move to Next Node
β
Step 4
If NULL
Stop
Else Repeat
π» Java Program β Traversing a Linked List
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
public class LinkedListTraversal{
public static void main(String args[]){
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
Node temp = head;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}
Output
10 20 30 40
π Dry Run
Initially
Head
β
10 β 20 β 30 β 40 β NULL
Iteration 1
temp = 10
Print 10
Move to 20
Iteration 2
temp = 20
Print 20
Move to 30
Iteration 3
temp = 30
Print 30
Move to 40
Iteration 4
temp = 40
Print 40
Move to NULL
Loop terminates.
π Time Complexity
Creating a Node
O(1)
Traversing
Every node is visited exactly once.
If there are n nodes,
Time Complexity
O(n)
πΎ Space Complexity
Traversal uses only one temporary reference.
O(1)
β Advantages of Singly Linked List
β Dynamic size
β Easy insertion
β Easy deletion
β Efficient memory utilization
β No need for contiguous memory
β Disadvantages
β Extra memory required for storing references
β Cannot move backward
β Searching is slower than arrays
β Random access is not possible
π Arrays vs Singly Linked List
| Feature | Array | Singly Linked List |
|---|---|---|
| Memory | Contiguous | Non-contiguous |
| Size | Fixed | Dynamic |
| Insertion | Slow | Fast |
| Deletion | Slow | Fast |
| Random Access | Yes | No |
| Memory Usage | May waste memory | Better utilization |
| Traversal | Easy | Sequential |
β Frequently Asked Questions
Q1. What is a Linked List?
A Linked List is a linear data structure where each element (node) stores data and a reference to the next node.
Q2. Why is a Linked List better than an Array?
Because it supports dynamic memory allocation and efficient insertion and deletion without shifting elements.
Q3. What is a Node?
A node is the basic building block of a linked list. It contains the data and the reference to the next node.
Q4. What is the Head in a Linked List?
The head is a reference to the first node of the linked list. Without it, the list cannot be accessed.
Q5. Can we access the third node directly?
No. In a singly linked list, nodes must be accessed sequentially starting from the head.
Q6. What does NULL indicate?
NULL indicates the end of the linked list. The last node’s next reference points to NULL.
π― Key Takeaways
- π A Linked List is a dynamic linear data structure.
- π¦ Each node contains data and a reference to the next node.
- π§ Nodes are stored in non-contiguous memory.
- π The head points to the first node.
- πΆ Traversal starts from the head and continues until
NULL. - β‘ Insertion and deletion are more efficient than in arrays.
- π Traversing a linked list takes O(n) time.
- πΎ Traversal uses O(1) extra space.
π Conclusion
A Singly Linked List is one of the most fundamental data structures in computer science. It addresses many of the limitations of arrays by allowing dynamic memory allocation and efficient insertion and deletion operations. Understanding how nodes are connected through references is essential before moving on to more advanced operations.
In this first part, you learned the core concepts of linked lists, including their structure, memory representation, node creation, and traversal. These concepts form the foundation for all linked list operations.
π In Part 2, we’ll build on this knowledge by implementing the most important operations on a singly linked list:
- β Insertion at the Beginning
- β Insertion at the End
- β Insertion at a Specific Position
- π§ͺ Dry runs and complete Java programs for each operation.