Professional Documents
Culture Documents
2022BCSE051
WEEK 5
01-04-2024
Q1 Write a program to create a single linked list. Include methods to insert elements at the
beginning, end, a specific position of the list, and a specific element of the list, delete
elements, search for a value, and display the contents of the list.
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data=data;
next=NULL;
};
if(head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
temp->next=n;
Yash Modi
2022BCSE051
int position=1;
node* temp=head;
temp=temp->next;
position++;
node* temp2=temp->next;
temp->next=n;
n->next=temp2;
int position=1;
bool flag=true;
while(head->next!=NULL){
position++;
if(head->data==value){
flag=false;
break;
head=head->next;
if(flag)
node* temp=head;
int count=0;
while(count!=pos){
temp=temp->next;
count++;
Yash Modi
2022BCSE051
return temp->data;
while(head->next!=NULL){
cout<<head->data<<" ";
head=head->next;
cout<<endl;
// int position=1;
while(head->data!=value){
head=head->next;
node* temp=head->next;
head->next=head->next->next;
// delete temp;
free(temp);
int position=1;
while(position=pos-1){
head=head->next;
node* temp=head->next;
head->next=head->next->next;
// delete temp;
free(temp);
int main() {
insert_at_tail(2,head);
insert_at_tail(3,head);
Yash Modi
2022BCSE051
insert_at_tail(4,head);
insert_at_tail(5,head);
insert_at_tail(6,head);
search_by_value(head,5);
insert_at_position(10,head,3);
show_list(head);
delete_at_position(head,5);
show_list(head);
cout<<search_by_position(head,3);
return 0;
Q2 Write a function to reverse a single linked list in-place. Implement an iterative solution to
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data=data;
next=NULL;
};
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
temp->next=n;
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
cout<<endl;
node* prev=NULL;
node* curr=head;
node* next=head->next;
while(curr!=NULL){
next = curr->next;
curr->next=prev;
prev=curr;
curr=next;
head=prev;
int main() {
insert_at_tail(2,head);
insert_at_tail(3,head);
insert_at_tail(4,head);
insert_at_tail(5,head);
insert_at_tail(6,head);
list_reversal(head);
show_list(head);
return 0;
Q3 Write a function that takes two sorted single linked lists as input and merges them into a single
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;}
while(head != NULL){
head = head->next;
void merge_list(node* head1, node* head2, node* &dummy, int size1, int size2){
int i = 0, j = 0;
temp3->next = temp2;
temp2 = temp2->next;
i++;
else{
Yash Modi
2022BCSE051
temp3->next = temp1;
temp1 = temp1->next;
j++;
temp3 = temp3->next;
temp3->next = temp1;
temp1 = temp1->next;
temp3 = temp3->next;
i++;
temp3->next = temp2;
temp2 = temp2->next;
temp3 = temp3->next;
j++;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
insert_at_tail(1, head2);
insert_at_tail(44, head2);
insert_at_tail(45, head2);
insert_at_tail(60, head2);
insert_at_tail(90, head2);
insert_at_tail(100, head2);
show_list(dummy->next);
return 0;
Q4 Write a function to remove the nth node from the end of a single linked list and return the head of the modified list.
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
}
Yash Modi
2022BCSE051
int position = 1;
head = head->next;
position++;
head->next = head->next->next;
delete temp;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
delete_at_position(head,2,6);
show_list(head);
return 0;
Q5 Write a function to find the intersection of two singly linked lists. If the lists do not
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
}
Yash Modi
2022BCSE051
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
node* temp=head;
int size=0,count=0;
int mid;
while(temp->next!=NULL){
temp=temp->next;
size++;
if(size%2==0){
mid=size/2;
if(size%2!=0){
mid=size/2;
mid++;
while(count!=mid){
temp=temp->next;
count++;
}
Yash Modi
2022BCSE051
cout<<"The position of the mid value is :"<<mid<<" and the data value is :"<<temp->data;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
show_list(head);
return 0;
Q6 Write a function to find the intersection of two singly linked lists. If the lists do not
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
int count=0;
while(head!=NULL){
head=head->next;
count++;
return count;
int flag=0;
int count=abs(size_checker(head1)-size_checker(head2));
if(size_checker(head1)>size_checker(head2)){
while(count!=0){
head1=head1->next;
count--;
if(head1==head2){
flag=1;
break;
head1=head1->next;
head2=head2->next;
else{
while(count!=0){
head2=head2->next;
count--;
if(head1==head2){
flag=1;
break;
head1=head1->next;
head2=head2->next;
if(!flag)
cout<<"No intersection";
else
cout<<"Intersection exists";
int main() {
head1->next = commonNode;
head2->next = commonNode;
insert_at_tail(20, head1);
insert_at_tail(30, head2);
intersection(head1, head2);
return 0;
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
Yash Modi
2022BCSE051
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
if(!head)
return;
temp->next = dummy;
dummy = temp;
reversal(head->next, dummy);
int flag=1;
while(dummy->next){
if(head->data==dummy->data)
flag=0;
else{
flag=1;
}
Yash Modi
2022BCSE051
head=head->next;
dummy=dummy->next;
if(flag==0)
cout<<"Pallindrome exists";
else{
cout<<"NO palindrome";
int main() {
insert_at_tail(2, head);
insert_at_tail(1, head);
insert_at_tail(1, head);
insert_at_tail(2, head);
insert_at_tail(1, head);
reversal(head,dummy);
pallindrome(head,dummy);
return 0;
Q8 Define the function moveToFront(struct node *head) to move a last node to the front of a
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
Yash Modi
2022BCSE051
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
node* temp=head;
while(temp->next->next!=NULL){
temp=temp->next;
temp->next->next=head;
head=temp->next;
temp->next=NULL;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
Yash Modi
2022BCSE051
tail_to_head(head);
show_list(head);
#include <bits/stdc++.h>
class node {
public:
int data;
node* next;
node(int data) {
this->data = data;
next = NULL;
};
if (head == NULL) {
return;
temp = temp->next;
temp->next = n;
head = head->next;
}
Yash Modi
2022BCSE051
return true;
return false;
current = current->next;
return true;
if (isNonDecreasing(head)) {
} else {
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
non_decreasing(head);
return 0;