Linkedlist - Zip: I Will Be Explaining About
Linkedlist - Zip: I Will Be Explaining About
zip
Introduction
In this article, I am going to discuss one of the most important Data Structures- Linked List.
Linked List
Advantage of Linked List
Types of Linked List
How to Create a Linked List
Various operations on Linked list.
Linked List is a linear data structure which consists of a group of nodes in a sequence. Each node
contains two parts.
Doubly Linked List: In a doubly linked list, each node contains two links - the first link
points to the previous node and the next link points to the next node in the
sequence.The prev pointer of the first node and next pointer of the last node will point to
null.
Circular Linked List: In the circular linked list, the next of the last node will point to the
first node, thus forming a circular chain.
Doubly Circular Linked List: In this type of linked list, the next of the last node will
point to the first node and the previous pointer of the first node will point to the last node.
The node for a Doubly Linked list will contain one data part and two link parts - previous link
and next link. Hence, we create a class definition of a node for the doubly linked list as shown
below.
Now, our node has been created, so, we will create a linked list class now. When a new Linked
List is instantiated, it just has the head, which is Null.The SinglyLinkedList class will contain
nodes of type Node class. Hence, SinglyLinkedList class definition will look like below.
The DoublyLinkedList class will contain nodes of type DNode class. Hence, DoublyLinkedList
class will look like this.
The first node, head, will be null when the linked list is instantiated. When we want to
add any node at the front, we want the head to point to it.
We will create a new node. The next of the new node will point to the head of the Linked
list.
The previous Head node is now the second node of Linked List because the new node is
added at the front. So, we will assign head to the new node.
To insert the data at front of the doubly linked list, we have to follow one extra step .i.e point the
previous pointer of head node to the new node. So, the method will look like this.
If the Linked List is empty, then we simply add the new node as the Head of the Linked
List.
If the Linked List is not empty, then we find the last node and make next of the last node
to the new node, hence the new node is the last node now.
The last node will be the one with its next pointing to null. Hence we will traverse the list until
we find the node with next as null and return that node as last node. Therefore the method to get
the last node will be
In the above mentioned method, pass doubleLinkedList object to get last node for Doubly
Linked List.
So the method for singly Linked List will look like this,
To perform this operation on doubly linked list we need to follow two extra steps
So, the method for Doubly Linked List will look like this.
So, the method for singly linked list will look like this,
To perform this operation on doubly linked list we don't need any extra pointer for previous node
as Doubly linked list already have a pointer to previous node.so the delete method will be,
This is one of the most famous interview questions. We need to reverse the links of each node to
point to its previous node, and the last node should be the head node.This can be achieved by
iterative as well as recursive methods. Here I am explaining the iterative method.
We need two extra pointers to keep track of previous and next node, initialize them to
null.
Start traversing the list from head node to last node and reverse the pointer of one node in
each iteration.
Once the list is exhausted, set last node as head node.