You are on page 1of 18

Yash Modi

2022BCSE051

WEEK 5

01-04-2024

Single Linked List

Aim: Implement the following operations on Single Linked List.

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>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data=data;

next=NULL;

};

void insert_at_start(int data,node* &head){

node* temp= new node(data);

if(head==NULL)

head=temp;

else{

temp->next=head;

head=temp;

void insert_at_tail(int data,node* &head){

node* temp=head;

while(temp->next!=NULL){

temp=temp->next;

node* n= new node(data);

temp->next=n;
Yash Modi
2022BCSE051

void insert_at_position(int data, node* &head,int pos){

int position=1;

node* temp=head;

while((position!=pos-1) && temp->next!=NULL){

temp=temp->next;

position++;

node* temp2=temp->next;

node* n= new node(data);

temp->next=n;

n->next=temp2;

void search_by_value(node* head,int value){

int position=1;

bool flag=true;

while(head->next!=NULL){

position++;

if(head->data==value){

cout<<"element found at : "<<position<<endl;

flag=false;

break;

head=head->next;

if(flag)

cout<<" Element not found sorry"<<endl;

int search_by_position(node* head, int pos){

node* temp=head;

int count=0;

while(count!=pos){

temp=temp->next;

count++;
Yash Modi
2022BCSE051

return temp->data;

void show_list(node* head){

while(head->next!=NULL){

cout<<head->data<<" ";

head=head->next;

cout<<endl;

void delete_at_value(node* head,int value){

// int position=1;

while(head->data!=value){

head=head->next;

node* temp=head->next;

head->next=head->next->next;

// delete temp;

free(temp);

void delete_at_position(node* head,int pos){

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() {

node* head=new node(1);

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

reverse the list.

#include<bits/stdc++.h>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data=data;

next=NULL;

};

void insert_at_tail(int data,node* &head){

node* temp=head;

while(temp->next!=NULL){

temp=temp->next;

node* n= new node(data);

temp->next=n;

void show_list(node* head){


Yash Modi
2022BCSE051

while(head!=NULL){

cout<<head->data<<" ";

head=head->next;

cout<<endl;

void list_reversal(node* &head){

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() {

node* head=new node(1);

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

sorted list. The original lists should remain unchanged.

#include<bits/stdc++.h>

using namespace std;


Yash Modi
2022BCSE051

class node{

public:

int data;

node* next;

node(int data){

this->data = data;

next = NULL;

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);

temp->next = n;}

void show_list(node* head){

while(head != NULL){

cout << head->data << " ";

head = head->next;

cout << endl;

void merge_list(node* head1, node* head2, node* &dummy, int size1, int size2){

int i = 0, j = 0;

node* temp1 = head1;

node* temp2 = head2;

node* temp3 = dummy;

while(i < size1 && j < size2){

if(temp1->data > temp2->data){

temp3->next = temp2;

temp2 = temp2->next;

i++;

else{
Yash Modi
2022BCSE051

temp3->next = temp1;

temp1 = temp1->next;

j++;

temp3 = temp3->next;

while(i < size1){

temp3->next = temp1;

temp1 = temp1->next;

temp3 = temp3->next;

i++;

while(j < size2){

temp3->next = temp2;

temp2 = temp2->next;

temp3 = temp3->next;

j++;

int main() {

node* head = new node(11);

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);

node* head2 = new node(2);

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);

node* dummy = new node(0);


Yash Modi
2022BCSE051

merge_list(head, head2, dummy, 5, 6);

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>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data = data;

next = NULL;

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);

temp->next = n;

void show_list(node* head){

while(head != NULL){

cout << head->data << " ";

head = head->next;

cout << endl;

}
Yash Modi
2022BCSE051

void delete_at_position(node* head, int nth, int size) {

int position = 1;

while (position != size-nth) {

head = head->next;

position++;

node* temp = head->next;

head->next = head->next->next;

delete temp;

int main() {

node* head = new node(11);

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

intersect, return null.

#include<bits/stdc++.h>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data = data;

next = NULL;

}
Yash Modi
2022BCSE051

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);

temp->next = n;

void show_list(node* head){

while(head != NULL){

cout << head->data << " ";

head = head->next;

cout << endl;

void find_mid(node* head){

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() {

node* head = new node(11);

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

intersect, return null.

#include<bits/stdc++.h>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data = data;

next = NULL;

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);


Yash Modi
2022BCSE051

temp->next = n;

int size_checker(node* head){

int count=0;

while(head!=NULL){

head=head->next;

count++;

return count;

void intersection(node* head1, node* head2){

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--;

while((head1 && head2)){

if(head1==head2){

flag=1;

break;

head1=head1->next;

head2=head2->next;

else{

while(count!=0){

head2=head2->next;

count--;

while((head1 && head2)){


Yash Modi
2022BCSE051

if(head1==head2){

flag=1;

break;

head1=head1->next;

head2=head2->next;

if(!flag)

cout<<"No intersection";

else

cout<<"Intersection exists";

int main() {

node* head1 = new node(1);

node* head2 = new node(2);

node* commonNode = new node(10);

head1->next = commonNode;

head2->next = commonNode;

insert_at_tail(20, head1);

insert_at_tail(30, head2);

intersection(head1, head2);

return 0;

Q7 Write an O(n) time function to determine if a single linked list is a palindrome.

#include<bits/stdc++.h>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data = data;
Yash Modi
2022BCSE051

next = NULL;

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);

temp->next = n;

void show_list(node* head){

while(head != NULL){

cout << head->data << " ";

head = head->next;

cout << endl;

void reversal(node* head, node* &dummy) {

if(!head)

return;

node* temp = new node(head->data);

temp->next = dummy;

dummy = temp;

reversal(head->next, dummy);

void pallindrome(node* head, node* 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() {

node* head = new node(10);

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);

node* dummy= new node(0);

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

single linked list.

#include<bits/stdc++.h>

using namespace std;

class node{

public:

int data;

node* next;

node(int data){

this->data = data;

next = NULL;
Yash Modi
2022BCSE051

};

void insert_at_tail(int data, node* &head){

node* temp = head;

while(temp->next != NULL){

temp = temp->next;

node* n = new node(data);

temp->next = n;

void show_list(node* head){

while(head != NULL){

cout << head->data << " ";

head = head->next;

cout << endl;

void tail_to_head(node* &head){

node* temp=head;

while(temp->next->next!=NULL){

temp=temp->next;

temp->next->next=head;

head=temp->next;

temp->next=NULL;

int main() {

node* head = new node(11);

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);

Q9 Write a program to check if the list is in non-decreasing order or not.

#include <bits/stdc++.h>

using namespace std;

class node {

public:

int data;

node* next;

node(int data) {

this->data = data;

next = NULL;

};

void insert_at_tail(int data, node* &head) {

if (head == NULL) {

head = new node(data);

return;

node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

node* n = new node(data);

temp->next = n;

void show_list(node* head) {

while (head != NULL) {

cout << head->data << " ";

head = head->next;

cout << endl;

}
Yash Modi
2022BCSE051

bool isNonDecreasing(node* head) {

if (head == NULL || head->next == NULL) {

return true;

node* current = head;

while (current->next != NULL) {

if (current->data > current->next->data) {

return false;

current = current->next;

return true;

void non_decreasing(node* head) {

if (isNonDecreasing(head)) {

cout << "Yes" << endl;

} else {

cout << "No" << endl;

int main() {

node* head = new node(11);

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);

cout << "Is the list in non-decreasing order? ";

non_decreasing(head);

return 0;

You might also like