0% found this document useful (0 votes)
0 views

Linked List

A linked list is a dynamic data structure consisting of nodes that contain data and pointers to the next node, allowing for efficient insertions and deletions. There are various types of linked lists, including singly, doubly, and circular linked lists, each with distinct traversal and manipulation methods. Linked lists are widely used in applications such as memory management, music playlists, and polynomial representation due to their flexibility and dynamic sizing.

Uploaded by

aissmsra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Linked List

A linked list is a dynamic data structure consisting of nodes that contain data and pointers to the next node, allowing for efficient insertions and deletions. There are various types of linked lists, including singly, doubly, and circular linked lists, each with distinct traversal and manipulation methods. Linked lists are widely used in applications such as memory management, music playlists, and polynomial representation due to their flexibility and dynamic sizing.

Uploaded by

aissmsra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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.

2 Flexible and Dynamic

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

Efficient Insertions and Deletions Dynamic Size

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.

Memory Allocation Flexibility Implementation Simplicity

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

Arrays S tacks and Queues Trees and Graphs

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

Singly Linked List

A basic linked list where each node has


a reference to the next node, forming a
unidirectional chain.
Types of Linked Lists

Doubly Linked List

Similar to a singly linked list, but each


node also has a reference to the
previous node, enabling bidirectional
traversal.
Types of Linked Lists

Circular Linked List

In this variation, the las t node of the lis t


points back to the firs t node, creating a
circular structure.
Operations on a Linked List

1 Traversal

Iterate through the linked list to access


and process each individual node or its
Insertion 2 data.
Insert new nodes at the beginning, end, or
any desired position in the linked list.
3 Deletion

Remove nodes from the linked list,


adjusting pointers to ensure proper
Search 4 connections.

Find specific data within the linked list by


traversing the nodes and comparing their
values. 5 Reversal

Reverse the order of the nodes in the


linked list, altering the sequencing from
head to tail.
Singly Linked List: Operations
struct Structure_Name{
Data_type info;
void display(struct node **q){
struct Structure_Name * link; struct node *temp;
}; temp=*q;
while(temp!=NULL){
void create/append(struct node **q, int num){ printf("%d  ",temp->data);
struct node *temp,*r; temp=temp->link;
if(*q==NULL){ }
temp=(struct node*)malloc(sizeof(struct node)); }
temp->link=NULL;
temp->data=num;
*q=temp; void addatbeg(struct node **q,int num){
} struct node *temp;
else{ temp=(struct node*)malloc(sizeof(struct
temp=*q; node));
while(temp->link!=NULL) temp->link=*q;
temp=temp->link; temp->data=num;
r =(struct node*) malloc(sizeof(struct node)); *q=temp;
r->data=num; }
r->link=NULL;
temp->link=r;
}
}
Singly Linked List: Operations

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

1 Memory Overhead 2 Traversal Complexity 3 Unsuitable for


Random Access
Each node in a linked list Locating a specific node
requires additional in a linked list requires Unlike arrays, linked lists
memory for the data iterating through the do not support direct
payload and the nodes from the access to elements based
next/previous node beginning until the on their indices, slowing
pointers. desired node is found. down element retrieval.
1. Dynamic Memory Allocation:
2. Implementation of Stacks, Queues, Trees, and Graphs:
3. Symbol Table in Compilers:
• Symbol tables store information about variables, functions, and other symbols in a program, and linked lists
facilitate efficient management and retrieval of this information.
4. Dynamic Memory Management in Operating Systems:
Applications of linked list

• 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.

1. Detect a Cycle in a Linked List (Floyd’s Cycle Detection Algorithm)


2. Find the Middle of the Linked List
3. Find the nth Node from the End of the Linked List
4. Remove Duplicates from a Sorted Linked List
1. Detect a Cycle in a Linked List (Floyd’s Cycle Detection Algorithm)

1. Initialize two pointers: slow = head and fast = head.

2. Move slow one step and fast two steps at a time.

3. If slow == fast, then there is a cycle in the list.

4. If fast or fast.next becomes null, then there is no cycle.


2. Find the Middle of the Linked List

1. Initialize two pointers: slow = head and fast = head.

2. Move slow one step and fast two steps at a time.

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

1. Move the fast pointer n steps ahead of slow.


2. Then, move both fast and slow one step at a time.
3. When fast reaches the end, slow will point to the nth node from the end.
4. Remove Duplicates from a Sorted Linked List

1. Use a pointer (current) to traverse the list.


2. Compare current with the current.next node.
3. If they are the same, skip the current.next node by updating the pointer, thus removing the duplicate.
Any question?

Thank you!
XOR Linked List

You might also like