Linked List
Linked List
S. K . Verma
Definition of a Linked List
1 A S equential Collection
A linked list is a linear data structure comprised of nodes that are connected through
pointers, forming a chain-like sequence.
Unlike arrays, linked lists can grow or shrink dynamically by adding or removing nodes
at any position.
3 Elementary Components
A node typically contains two key elements: the data it stores and a reference to the
next node in the list.
Advantages of Using a Linked List
Linked lists enable efficient insertion and The ability to grow or shrink dynamically
deletion operations by simply adjusting the makes linked lists ideal for scenarios where
pointers, without requiring data movement. the number of elements changes frequently.
Nodes in a linked list can be scattered Implementing and manipulating linked lists is
throughout the memory, allowing for flexible relatively straightforward, making them
memory allocation and efficient use of space. popular in programming.
Advantages of Using a Linked List
Comparison with Other Data S tructures
Linked lists offer dynamic Stacks and queues can be Linked lists serve as the
size and efficient implemented using linked foundation for more
insertions/deletions, lists, providing dynamic complex data structures like
whereas arrays provide behavior and efficient trees and graphs,
faster random access and FIFO/LIFO operations. facilitating node linkage.
contiguous memory.
Types of Linked Lists
1 Traversal
void addafter(struct node **q,int loc, int num){ void delete(struct node **q,int num){
struct node *temp,*r; struct node *temp,*pre;
int i; temp=*q;
temp=*q; while(temp!=NULL){
for(i=0;i<loc-1;i++){ if(temp->data==num){
temp=temp->link; if(temp==*q)
if(temp==NULL){ *q=temp->link;
printf("no. of nodes are less than the position else
to insert a new node"); pre->link=temp->link;
return; free(temp);
} return;
} }
r=(struct node*)malloc(sizeof(struct node)); else{
r->data=num; pre=temp;
r->link=temp->link; temp=temp->link;
temp->link=r; }
} }
printf("element %d not found",num);
}
Singly Linked List: Operations
Sorting
Singly Linked List: Operations
void merge(struct node *p, struct node *q, struct
node **s){
struct node *z;
z=NULL;
if(p==NULL && q==NULL)
return;
while(p!=NULL && q!=NULL){
if(*s==NULL){ while(p!=NULL){
*s=malloc(sizeof(struct node)); z->link=malloc(sizeof(struct node));
z=*s; z=z->link;
} z->data=p->data;
else{ p=p->link;
z->link=malloc(sizeof(struct node)); }
z=z->link; while(q!=NULL){
} z->link=malloc(sizeof(struct node));
if(p->data<q->data){ z=z->link;
z->data=p->data; p=p->link; z->data=q->data;
} q=q->link;
else if(q->data<p->data){ }
z->data=q->data; q=q->link; z->link=NULL;
} }
else{
z->data=p->data; p=p->link; q=q->link;
}
}
Polynomial Representation Using Linked List
void polyadditon (struct node *p, struct node *q, struct node Polynomial 10x^4 + 5x^2 + 25 is represented as following:
**s){
struct node * temp;
if (q == NULL && p ==NULL){
return;
}
while ( p ! = NULL && q! = NULL){
if (*s == NULL){ while (p ! = NULL){
*s = malloc (size of (struct node)); if (* s == NULL)
temp = * s;} * s=malloc(sizeof(struct node)); temp = *s;
else{ else {
temp → link = malloc (size of (struct node)); temp → link = malloc (size of (struct
temp = temp → link; node)); temp = temp → link;
} }
if (p → exp < q → exp){ temp → coef = p → coef;
temp → coef = q → coef; temp → exp = p → exp;
temp → e × p = q → e × p; p = p → link; }
q = q → link;} while (q ! = NULL){
else { if (*s == NULL){
if (p → exp > q → exp){ *s = malloc (sizeof(struct node));
temp → coef = p → coef; temp = *s ;
temp → exp = p → exp; }
p = p → link; else {
} temp →link=malloc(sizeof(struct
else { node));
temp → coef = p + coef + q → coef; }
temp → exp = q → exp; }
q = q → link ; }
p = p → link;
}}}
Common Problems and Challenges with Linked Lists
• In operating systems, linked lists are commonly used to manage free blocks of memory.
• Memory allocation and deallocation can be efficiently handled by maintaining linked lists of free memory
blocks.
5. Music Player Playlist:
• Linked lists are suitable for representing playlists in music players.
• Each node in the linked list represents a song, and the links between nodes define the order of the playlist.
6. Hash Table Chaining:
• Linked lists are used in combination with hash tables for collision resolution through chaining.
• In case of a hash collision, elements with the same hash value can be stored in a linked list attached to the
corresponding hash table index.
7. Polynomial Representation:
• Linked lists can be used to represent polynomials efficiently.
• Each node represents a term in the polynomial, with the links indicating the degree of the term.
8. Job Scheduling in Operating Systems:
• Linked lists are employed in job scheduling algorithms in operating systems.
• Each node represents a job, and the links define the scheduling order.
Example: Write a function to get the intersection point of two Linked Lists
There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got
linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge.
Approach-1
• Use 2 nested for loops.
• The outer loop will be for each node of the 1st list and the inner loop will be
for the 2nd list.
• In the inner loop, check if any of the nodes of the 2nd list is the same as the
current node of the first linked list.
The time complexity of this method will be O(M * N) where M and N are the
numbers of nodes in two lists.
Space Complexity = O(1)
Example: Write a function to get the intersection point of two Linked Lists
There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got
linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge.
Approach-2
• Create an empty hash set.
• Traverse the first linked list and insert all nodes’ addresses in the hash set.
• Traverse the second list. For every node check if it is present in the hash set. If
we find a node in the hash set, return the node.
The time complexity of this method will be O(N) where M and N are the
numbers of nodes in two lists.
Space Complexity = O(M), Let N > M
Example: Write a function to get the intersection point of two Linked Lists
There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got
linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge.
Approach-3
• Get the count of the nodes in the both lists, let the count be c1, c2.
• Get the difference of counts d = abs(c1 – c2)
• Now traverse the bigger list from the first node to d nodes so that from here
onwards both the lists have an equal no of nodes
• Then traverse both lists in parallel till a common node encountered.
Time Complexity: O(M+N)
Auxiliary Space: O(1)
Two Pointers Technique
The two pointers technique is commonly used in problems involving linked lists and
arrays. When applied to a linked list, this approach involves using two pointers that
move at different speeds or with different strategies to solve various problems
efficiently.
3. If fast or fast.next becomes null, then slow will be at middle of the list.
3. Find the Nth Node from the End of the Linked List
Thank you!
XOR Linked List