Linked List Notes
Linked List Notes
SZABIST University
Linked List is a data structure. A data structure that is non-continuous and dynamic sized and
non-indexed data structure. This means once declared you can keep adding more times as
opposed to static array data structure. If linked list is of n = 10 size initially, then you can still store
more values in it. You can change size of same Linked List object / variable. Since, Linked List is
not indexed so you don’t have to worry about it being continuous streamed data structure such
as array 1.
Linked list works by maintaining pointers as opposed to array which uses indexing. Pointers
points to next node and next node points to next node and so on. Hence, its also important to
know that you cannot directly access any node. You must traverse from start to reach specific
node.
Advantages:
• Dynamic Sized
o Has no size limit.
o During runtime, its size increases
• Non-Continuous
o Since its dynamic it’s not placed in memory in sequential format
o Half data starts at index 100 of memory
o Rest data starts at index 35350 of memory
1
Linked list is A non-continuous streamed, index less collection of dynamic sized & similar typed data
structure
Page | 1
Healthy life requires Healthy Algorithm
SZABIST University
• Memory Efficiency
o Only allocates memory as per size of linked list currently containing values
o No empty nodes to waste memory
Disadvantages:
o You cannot access any element directly. You either start from 1st node or end
node if it’s doubly linked list 0(N)
Time Complexity:
In Linked List adding item at beginning requires only 1 operation, which is as follows:
• Update head node value and make new node as first node.
• Now the first node will point to the rest of Linked List
However, adding item (node) at end of Linked List or at position is always going to big O (n)
operations. The reason is simple traversal to last node requires to travel from beginning to end of
the Linked List and then just update pointer of last node to point to next new node. New node at
end will point to null.
Same case for deletion as adding. But searching node requires traversal of each node until you
reach the specific node containing desired data.
Page | 2
Healthy life requires Healthy Algorithm
SZABIST University
Each node pointing to next node in forward direction and so on till you reach null pointer
which is end of linked list
Each node pointing to next node as well as previous node. This means that now you can
traverse in backward as well as forward direction and so on till you reach null pointer
which is end of linked list.
3. Circular
Each node pointing to next node in forward direction and so on till you reach end
pointer which is now pointing to first node instead of null. Hence, there is no end of
link list via null. But when last node points to first node you realize you are at end of
link list.
Page | 3
Healthy life requires Healthy Algorithm
SZABIST University
Each node pointing to next node in forward direction as well as backward direction
and so on till you reach end pointer which is now pointing to first node and first node
now pointing to last node. So, you identify last node when last node points to first
node. There is no null node now if linked list is filled with at least 2 nodes or more.
In circular linked list if there is one node in linked list then last and first point to same
node. if there are no node then head pointer points to null just like normal non-circular
linked list.
Implementation:
class Node {
this.data = data;
}
}
By using abstract datatype, we create a node class that has next, previous and data fields for each
node. Each node will have these fields no matter what. They may point to null but there will always
be next and data node if its singly and previous also if its doubly linked list.
Page | 4
Healthy life requires Healthy Algorithm
SZABIST University
Here Node is not just class but a type as any class. So, Node next or Node previous code
statement means with one object of node class we are creating more node objects as shown
below.
Here,
Here,
Page | 5
Healthy life requires Healthy Algorithm
SZABIST University
As discussed before its last node points to first node. The code below is tricky but ill try to
explain
Node1.next.prev = Node1;
Node1.next.next = Node1;
Node1.displayList();
Here,
• You add new node by pointing to next node by telling Node1 that Node1’s next
• You tell 2nd node to point to previous node which is first node
• You tell 2nd node to point to next node which is first node in case of circular instead
Note: if there is 3rd node then 2nd node’s next pointer will be 3rd node and 3rd
node’s next pointer will be first node and so on. Last node will never point to null in
case of circular
Page | 6
Healthy life requires Healthy Algorithm
SZABIST University
//here next is new node and new nodes previous pointer is set to first node
Node1.next.prev = Node1;
//here next is new node and new nodes next pointer is set to 1st node
Node1.next.next = Node1;
//here Node1 is first node and its previous pointer is set to last node
Node1.prev= Node1.next;
Node1.displayList();
It is as shown below
Result will be
1. 1st node points to 2nd node and 1st node points to last node. In the above example
2nd node is also last node of the linked list. So, first node previous pointer will also
point to 2nd node aka last node.
2. 2nd node or last node will point to first node and 2nd node previous points to 1st node
also.
Here,
Page | 7
Healthy life requires Healthy Algorithm
SZABIST University
Note: if there is 3rd node then 2nd node’s next pointer will be 3rd node and 3rd
node’s next pointer will be first node and so on. Last node will never point to null in
case of circular
Practice Questions
package com.company;
class Node {
// Data part of the node
Node next; // Pointer to the next node
Node previous; // Pointer to the previous node
Node head; // The first node of the linked list data structure
Node last; // last node of link list
int data;
Page | 8
Healthy life requires Healthy Algorithm
SZABIST University
}
}
4. If Next element has same data and is duplicate remove from linked list
Page | 9
Healthy life requires Healthy Algorithm
SZABIST University
class Node {
int data;
Node next;
Node(int new_data) {
data = new_data;
next = null;
}
}
// Given the head of a list, reverse the list and return the
// head of reversed list
public class Main {
static Node reverseList(Node head) {
// Store next
next = curr.next;
Page | 10
Healthy life requires Healthy Algorithm
SZABIST University
head = reverseList(head);
6. Create a function where while calling you just tell which node you want to delete, and it
deletes that node
package com.company;
import java.util.ArrayList;
import java.util.LinkedList;
class Node {
// Data part of the node
Node next; // Pointer to the next node
Node head; // The first node of the linked list data structure
int data;
Page | 11
Healthy life requires Healthy Algorithm
SZABIST University
while(curr != null){
// we are simply converting next node to prev node creating a reverse cycle
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public static void main(String[] args) {
Node linkList1 = new Node(25);
linkList1.head = linkList1;
linkList1.next = new Node(254);
linkList1.next.next = new Node(125);
linkList1.next.next.next = new Node(113);
linkList1 = reverse(linkList1);
displayList(linkList1);
}
}
9. Print the middle node of linked list. If its even sized linked list print the right node of the
middle. Such as following
Page | 12