Overview of Linked List
A linked list is a
fundamental data structure in computer science that represents a linear
collection of elements.
Unlike arrays,
linked lists do not store elements in contiguous memory locations.
Instead, each
element (node) in a linked list contains a data element and a reference (link
or pointer) to the next node in the sequence.
The last node
typically points to null, indicating the end of the list.
key concepts
related to linked lists:
Basic Components
of a Linked List:
1. Node:
- A fundamental building block of a linked
list.
- Contains two fields: data and a reference
(or link) to the next node.
2. Head:
- The first node in the linked list.
- Serves as the entry point for accessing
the elements in the list.
3. Tail:
- The last node in the linked list.
- Its "next" reference is usually
set to null.
Types of Linked Lists:
1. Singly Linked
List:
- Each node points to the next node in the
sequence.
- Efficient for forward traversal.
Head -> Node1 -> Node2 -> Node3
-> ... -> Tail -> null
2. Doubly Linked
List:
- Each node has references to both the next
and the previous nodes.
- Enables backward traversal.
Head <-> Node1 <-> Node2
<-> Node3 <-> ... <-> Tail <-> null
3. Circular Linked
List:
- The last node points back to the first
node, forming a closed loop.
Head -> Node1 -> Node2 -> Node3
-> ... -> Tail -> Head
Linked List Operations:
1. Insertion:
- Adding a new node to the list.
- Three scenarios:
- Insert at the beginning: Adjust the
"next" reference of the new node and update the head.
- Insert in the middle: Adjust the
"next" references of the surrounding nodes.
- Insert at the end: Update the
"next" reference of the current tail and set the new node as the new
tail.
2. Deletion:
- Nodes can be deleted from a linked list.
- Deleting the first node requires updating
the head.
- Deleting the last node involves updating
the reference of the second-to-last node.
- Deleting a node in the middle requires
adjusting the references of neighboring nodes.
3. Traversal:
Traversing a linked list involves visiting
each node in the sequence.
- This is typically done using a loop that
starts at the head and iterates through the nodes until the end is reached.
4. Search:
- Finding a specific value in the linked
list.
- Requires traversing the list until the
value is found or reaching the end (null).
5. Updation:-
- Finding a specific value in the linked
list.
- Requires traversing the list until the
value is found or reaching the end (null).
- Replace old value to new value.
=============================================================
0 Comments