Linked List
Linked List
Linked list is a linear data structure that includes a series of connected nodes. Linked list can be
defined as the nodes that are randomly stored in the memory. A node in the linked list contains
two parts, i.e., first is the data part and second is the address part. The last node of the list
contains a pointer to the null. After array, linked list is the second most used data structure. In a
linked list, every link contains a connection to another link.
To overcome these limitations Linked Lists are used. In a link list the elements are
logically adjacent needs not to be physically adjacent in the memory, but they
should be linked or connected through a pointer.
Type of link List
You have to start somewhere, so we give the address of the first node a special name called
HEAD. Also, the last node in the linked list can be identified because its next portion points to
NULL.
Example:
In the following diagram of a one-way link list have four nodes. Each node has two parts.
The left part represents the information part of the node and the right part represents pointer
field or link field, which contains the memory address of the next node. In the list we have a
start node whose address is stored in a pointer START. We need a START pointer to trace the
list. A special case is the list that when the list has no nodes. Such a list is called a null list or
empty list and denoted by null pointer in the START pointer.
START
1000
A big benefit with using linked lists is that nodes are stored wherever there is free space in
memory, the nodes do not have to be stored contiguously right after each other like elements are
stored in arrays. Another nice thing with linked lists is that when adding or removing nodes, the
rest of the nodes in the list do not have to be shifted.
algorithms:
1. Insertion Operation:
Let us suppose LIST be a one-way link list with N successive nodes and a node
insert is to be inserted in a link list. The given node can be inserted in a link list in
the following three locations.
➢ Front Insertion
➢ Middle Insertion
➢ End Insertion
I. Front Insertion:
It is the easiest way to insert a node in the link list. Following algorithm is used to insert
a
node in the front of the LIST.
ALGORITHM: FRONT INSERTION (Insert, Node, First, Data, Info) This algorithm is used
to insert a node insert at the start of a one-way link list.
Step 1: [Create a node and assign it to insert]
II. Middle Insertion: There are two methods, which can be used to insert node at
the middle if a one-way link list.
Method 1: If the nodes of the given list are unsorted then we insert node in a list after
a given node.
Method 2: If the nodes of the given list are sorted in some particular order,
then the node is inserted at a particular position so that the sorted
order could be maintained.
Here is discussed the second method. The following algorithm inserts a node at a proper
place.
ALGORITHM: MIDDLE INSERTION (Prev, First, Cur, X, Insert)
This algorithm is used to insert a node at the proper location in as ordered LIST
I. End Insertion:
By end insertion, means to insert a node at the end/last of a link list. The
algorithm is given as follow:
2. Deletion Operation:
Let LIST be a given link list with N successive nodes and we want to delete a node
X from a link list. Then we can delete that node from three different location of a
link list, which is given as:
➢ Front Deletion
➢ Middle Deletion
➢ End Deletion
I. Front Deletion: Front deletion is the easiest way to delete a node from a link
list. Following is the algorithm is used to delete a node from one-way link list from
the front or start.
This algorithm is used to delete a node from the front of a one-way link list.
I. Middle Deletion: In middle deletion if we want to delete node X from a linked list
which is not at the first or last location. For this we start searching for that node in a
list. If found then we should update successor and predecessor and then delete the
node. The following algorithm is used to delete a node from middle form one-way
link list.
I. End Deletion: To delete the end node of a one-way link list first we have to reach to
the end of the list.
In the above algorithm in step 1 & 2 we assign prev to first node and cur to the next
node to the prev. In step 3, 4 & 5, we reach to the end of the list. In step 6 & 7, we
assign Null to prev to make it the end node and free cur pointer to delete the end
node.
The following figure shows the above mechanism.
FIRST
1000
Time Complexity
Accessing an Element (Search)
o Explanation: The best case occurs when the element you are looking for is at the
head of the list.
o Explanation: On average, you may need to search through half the elements to
find the target, but the complexity is still linear because of the linear nature of
traversal.
o Explanation: The worst case occurs when the element is at the end of the list or
not present at all, requiring traversal of the entire list.
Inserting an Element
• At the Beginning:
• At the End:
▪ Explanation: Without a tail pointer, you must traverse the entire list to
find the last node, making it linear time.
• At a Specific Position:
▪ Explanation: For other positions, you need to traverse the list to reach the
specific position, making it linear time.
3. Deleting an Element
• At the Beginning:
• At the End:
o Best Case: O(1) (if there’s a tail pointer and a doubly linked list structure or
reference to the previous node)
▪ Explanation: Without a tail pointer or with a singly linked list, you must
traverse the list to find the second-to-last node to delete the last node,
making it linear time.
• At a Specific Position:
▪ Explanation: For other positions, you must traverse the list to reach the
specific position, making it linear time.
4. Updating an Element
o Explanation: You need to traverse the list to the specific position, which is linear
time in the average and worst cases.
TWO WAY LINKED LIST
Introduction to Two Way Linked List/ Double Linked List:
One of the big disadvantages of one-way link list is that only possible way to traverse
the data is in the list is the forward traversing. There is no backward traversing of
a list in one-way link list. The problem is handled by the double or two-way link list.
In two-way link list each node is linked to both its successor and predecessor. The
two-way link list is traversable from either direction i.e. forward and backward. On
two-way link list a node is divided into three parts.
➢ Information part: It contains the data of a node.
➢ Right or next pointer: It points to the successor node.
➢ Left or previous pointer: Pointer to the predecessor node.
In the following diagram of two-way link list with three nodes, each node has three
parts. The left part contains the address of predecessor node while the right part
contains the address of the successor node and the information part contains the
information about the element. There are two pointers also used i.e. FIRST and
LAST. The FIRST pointer points to the first or start node of the two-way link list
and the LAST pointer points to the last or end node of the two-way link list.
I. Front Insertion:
Following algorithm is used to insert a node in the front/start of the Two-way link list.
I. Middle Insertion:
There are two methods that can be used to insert node at the middle of a Two-way link list.
Method 1: If the nodes of the given list are unsorted then we insert node in a list after
a given node.
Method 2: If the nodes of the given list are sorted in some particular order then
the node is inserted at a particular position so that the sorted order
could be maintained. We will discuss the second method. The
following algorithm inserts a node at a proper place in Two-way link
list.
ALGORITHM: MIDDLE INSERTION (Prev, Cur, First, Insert)
This algorithm is used to insert a node at the proper location in an ordered LIST in
Two-way link list.
In the above algorithm step 4, 5 & 6 are used to find out a proper position for a new node. In
step 7 & 8, we create a new node, assign its address to insert pointer and store information.
In step 9, 10 & 11, we link the new node to the current and previous nodes respectively.
The following figure shows the above mechanism
End Insertion:
End insertion in the two-way link list is much easier than the one –way link list
because we have a direct access to the end node of the two-way link list node.
Let LIST be a given Two-way link list with N successive nodes and we want to delete
a node X from LIST. Then we can delete that node from three different location of a
Two-way link list which is given as:
I. Front Deletion
II. Middle Deletion
III. End Deletion
I. Front Deletion:
Front deletion is the easiest way to delete a node from a link list. Following is the
algorithm which is used to delete a node from Two-way link list from the front or start.
ALGORITHM: FRONT DELETION (Cur, First)
This algorithm is used to delete a node from the front of a Two-way link list.
In the above algorithm in step 1, we store the address of first node in current
pointer. In step 2, we update the FIRST to the next node to make the second node
as first. In step 3, we delete the node by free () function provided in C language. In
step 4, again we set the current pointer to point to first node and now current node
is the first node in the list. In step 5, we assign NULL to current left pointer to
make it first node of the list.
I. Middle Deletion:
Let LIST be a Two-way link list with N successive nodes and we want to delete a
node X from a linked list which is not at the FIRST or LAST location. For this we
start searching for that node in the list. If found then one should update the successor
and predecessor node pointers.
The following algorithm inserts a node at a proper place in Two-way link list.
ALGORITHM: MIDDLE DELETION (Prev, Cur, First)
This algorithm is used to delete a node at the proper location in Two-way link list.
In the above algorithm in step 1, 2, we assign previous pointer to first node and cur
to the next of first node. In step 4, 5, 6 & 7, we start loop for searching for the node,
if it is found it is deleted and exited otherwise the prev is updated to cur and cur is
updated to the next node at right side.
I. End Deletion:
End deletion is also very easy as front deletion in Two-way link list. Following algorithm is
used to delete end node of Two-way link list.
ALGORITHM: END DELETION (Cur, Last)
This algorithm is used to delete the end node of a Two-way link list.
In step 2 of the above algorithm we updated the LAST pointer to point the 2nd last
node in the list. In step 3, memory is free occupied by the last node. In step 4, again
set current pointer to last node, and now current node is the last node in the list.
Therefore, assign NULL to current right pointer.
Here, the address of the last node consists of the address of the first node.
Step-by-step approach:
• Create a new node with the given value.
• Check Empty List (last == nullptr):
o Make newNode->next point to itself.
• Insert at Beginning:
o Set newNode->next to last->next.
o Update last->next to newNode.
Step-by-step approach:
• If last is nullptr and pos is not 1, print “Invalid position!“.
• Otherwise, Create a new Node with given data.
• Insert at Beginning: If pos is 1, update pointers and return last.
• Traverse List: Loop to find the insertion point; print “Invalid position!” if out of bounds.
• Insert Node: Update pointers to insert the new node.
• Update Last: If inserted at the end, update last.
Time Complexity: O(n), we have to traverse the list to find the specific position.
Circular Doubly Linked List has properties of both doubly linked list and circular linked list in
which two consecutive elements are linked or connected by the previous and next pointer and the
last node points to the first node by the next pointer and also the first node points to the last node
by the previous pointer.
List initially contains some nodes, start points to the first node of the List:
A node(Say M) is inserted with data = 7, so the previous pointer of M points to the last node, the
next pointer of M points to the first node and the last node’s next pointer points to this M node,
and first node’s previous pointer points to this M node.
Insertion at the beginning of the list:
To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer
points to the first node of the list, T previous pointer points to the last node of the list, last node’s
next pointer points to this T node, first node’s previous pointer also points this T node and at last
don’t forget to shift ‘Start’ pointer to this T node.
Header Linked List is a modified version of Singly Linked List. In Header linked list, we have a
special node, the Header Node present at the beginning of the linked list. The Header Node is an
extra node at the front of the list storing meaningful information about the list. Such a node is not
similar in structure to the other nodes in the list. It does not represent any items of the list like
other nodes rather than the information present in the Header node is global for all nodes such
as Count of Nodes in a List, Maximum among all Items, Minimum value among all Items etc.
This gives useful information about the Linked list.
Now, Header Linked List is of two types:
• Grounded Header Linked List
• Circular Header Linked List.
Grounded Header Linked List
In this type of Header Linked List, the last node of the list points to NULL or holds the reference
to NULL Pointer. The head pointer points to the Header node of the list. If the there is no node to the next
of head pointer or head.next equals NULL then we know that the Linked List is empty. The operations
performed on the Header Linked List are same as Singly Linked List such as Insertion, Deletion, and
Traversal of nodes. Let us understand this with an example:
The above image shows a Grounded Header Linked List. We can see the Header Node at the beginning
followed by the actual nodes of the list where the last node points to NULL. The header node maintains
global information about the list. Firstly, we have to create Header Node first whose data field will be
NULL or 0 initially. After that we can perform any operations on that linked list and store the global
information about the linked list on the header node. Here, we store the count or Total no. of nodes
present in the Linked List, the Maximum and Minimum value among all nodes in the list. So, in the above
Linked List Total Nodes are 4 , Maximum Value is 99 and Minimum value is 5 which are present as fields
in Header Node.
We can see the last node of the list points to the header node and not the node 99. There might be problem
in traversing through the nodes. It is because the structure of the last node and header node are different
which might cause an Type Mismatch Exception. So while traversing we fix the Header node and keep on
traversing until node. Next points to header node. The rest features of the Header remains same as its
Grounded counterpart.