You are on page 1of 13

Linked List

Ali Hassan
Linked List
• A linked list in Java is a linear data structure that consists of a
sequence of elements, where each element is connected to the next
one through a reference (or link). Unlike arrays, linked lists do not
require contiguous memory allocation, which makes them more
flexible for dynamic data storage.
Types of Linked Lists
• Singly Linked List: Each node has a reference to the next node.
• Doubly Linked List: Each node has references to both the next and
previous nodes.
• Circular Linked List: The last node points back to the first node,
forming a closed loop.
• Circular Doubly Linked List: The last node points back to the first
node and each node has references to both the next and previous
nodes forming a closed loop.
Singly Linked List
Head
tail
4800
4000

Data Link

5 5 4900 15 5000 25 4000 30 null

4800 4900 5000 4000

Node
Adding Elements
• addFirst(T data): Adds an element to the beginning of the list.
• addLast(T data): Adds an element to the end of the list.
• insertAt(int index, T data): Inserts an element at a specific position.
Removing Elements
• removeFirst(): Removes and returns the first element.
• removeLast(): Removes and returns the last element.
• removeAt(int index): Removes and returns the element at a specific
position.
Accessing Elements
• get(int index): Returns the element at a specific position.
Other Operations
• size(): Returns the number of elements in the list.
• isEmpty(): Checks if the list is empty.
• clear(): Removes all elements from the list.
Implementation Of Linked List
• We can implement linked lists using two primary classes
• Node (to represent individual elements) and a LinkedList class (to
manage the list)
Node
• A Node is the basic building block of a linked list. It typically consists
of two components:
• Data: The actual value or data to be stored in the node.
• Next Reference: A reference (or link) to the next node in the
sequence. In a singly linked list, nodes have a single reference
pointing to the next node. In a doubly linked list, nodes have
references to both the next and previous nodes
LinkedList
• The LinkedList class is used to manage the linked list. It contains
references to the first (head) and last (tail) nodes of the list, as well as
methods for adding, removing, and accessing elements.
Advantages
• Dynamic Sizing: Linked lists can grow or shrink as needed without
wasting memory.
• Efficient Insertions and Deletions: Adding or removing elements in
the middle of a linked list is efficient.
• No Fixed Size: Unlike arrays, linked lists don't have a fixed size.
Disadvantages
• Inefficient Random Access: Accessing elements by index is less
efficient than with arrays because you need to traverse the list.
• Additional Memory Overhead: Each node has a reference, which
adds memory overhead compared to arrays.
• Not Cache-Friendly: Due to the scattered memory allocation of
nodes, linked lists may not utilize CPU cache efficiently.

You might also like