Java Programming — Study Notes

Unit 05

Linked List in Java

1 Question 6 Marks Java
01
6 Marks Question

What is Linked List? Explain Singly Linked List in Detail

Introduction to Linked List

A linked list is a linear data structure in which elements are stored in the form of nodes connected through links (pointers). It allows dynamic memory allocation and efficient insertion and deletion.

Definition: A linked list is a sequence of nodes where each node contains data and a reference (link) to the next node in the sequence.

Basic Components of a Linked List

Node
Node (Link)
The basic unit — stores the actual data value.
Next Pointer
Points to the address of the next node in the sequence.
H
Head (First)
A special pointer that points to the first node of the list.
Last Node
Points to NULL to indicate the end of the list.

Singly Linked List

A singly linked list is a type of linked list where each node contains a data field and one pointer (next) pointing to the next node. Traversal is possible only in the forward direction.

Structure of a Singly Linked List

Visual Representation
HEAD
10data
●→next
20data
●→next
30data
●→next
NULL
  • Each node has the form: [data | next]
  • The first node is accessed using head.
  • The last node points to NULL indicating end of list.

Basic Operations

Insertion
Add a new node at the beginning, middle, or end of the list.
Deletion
Remove an existing node from the list.
Traversal (Display)
Visit each node from head to NULL to display all elements.
Search
Find a specific element by traversing through the nodes.

Example — Java Representation

Java Code
class Node {
    int data;
    Node next;
}

class LinkedList {
    Node head;

    void insert(int data) {
        Node newNode = new Node();
        newNode.data = data;
        newNode.next = head;
        head = newNode;
    }
}

Explanation

  • Node class holds the data value and a next reference to the next node.
  • LinkedList class maintains the head pointer to the first node.
  • insert() adds a new node at the beginning by pointing the new node's next to the current head, then updating head.

Advantages & Disadvantages

✓ Advantages
  • Dynamic size — no fixed size limit
  • Efficient insertion and deletion
  • No memory wastage
✗ Disadvantages
  • No random access — sequential only
  • If head is lost, entire list is lost
  • Extra memory required for pointer
Conclusion A singly linked list is a dynamic linear data structure where nodes are connected sequentially using pointers. It is widely used where frequent insertion and deletion operations are required.