0% found this document useful (0 votes)
22 views51 pages

File

Uploaded by

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

File

Uploaded by

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

linked list code -> deletion, insertion operations on linked list

#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
public:
Node(int data2)
{
data = data2;
next = nullptr;
}
};

Node* convertarraytoLL(vector<int> &arr)


{
Node* head = new Node(arr[0]);
Node* mover = head;
for(int i=1;i<arr.size();i++)
{
Node* temp = new Node(arr[i]);
mover->next = temp;
mover = temp;
}
return head;
}

int lengthofLL(Node* head)


{
int count=0;
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
count++;
}
cout<<endl;
return count;
}

int checkIfpresent(Node* head, int val)


{
Node* temp = head;
while(temp!=NULL)
{
if(temp->data==val)
{
return 1;
}
temp = temp->next;
}
return 0;
}
Node* deletehead(Node* head)
{
Node* temp = head;
head = head->next;
free(temp);
//delete temp;
return head;
}

Node* deletetail(Node* head)


{
if(head==NULL||head->next==NULL)
{
return NULL;
}
Node* temp = head;
while(temp->next->next!=NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next = nullptr;
return head;
}
Node* deletektheelement(Node* head, int k)
{
if(head==NULL||head->next==NULL)
{
return NULL;
}
if(k==1)
{
Node* temp = head;
head = head-> next;
free(temp);
return head;
}
int count = 0;
Node* temp = head;
Node* prev = NULL;
while(temp!=NULL)
{
count++;
if(count==k)
{
prev->next = prev->next->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;
}

Node* deletelementequaltoval(Node* head, int val)


{
if(head==NULL)
{
return NULL;
}
if(head->data==val)
{
Node* temp = head;
head = head-> next;
free(temp);
return head;
}
Node* temp = head;
Node* prev = NULL;
while(temp!=NULL)
{
if(temp->data==val)
{
prev->next = prev->next->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;
}

Node* newhead(Node* head, int val)


{
Node* temp = new Node(val,head);
return temp;
}

Node* inserttail(Node* head, int val)


{
if(head==NULL)
{
return new Node(val,head);
}
Node* temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
Node* newNode = new Node(val);
temp->next = newNode;
return head;
}

Node* insertatkthposition(Node* head, int val, int k)


{
if(head==NULL)
{
if(k==1)
{
return new Node(val);
}
else
{
return NULL;
}
}
if(k==1)
{
Node* temp = new Node(val,head);
return temp;
}
int count = 0;
Node* temp = head;
while(temp!=NULL)
{
count++;
if(count==k-1)
{
Node* x = new Node(val,temp->next);
temp->next = x;
break;
}
temp = temp->next;
}
return head;
}

Node* insertbeforevaluek(Node* head, int val, int k)


{
if(head==NULL)
{
return NULL;
}
if(head->data==k)
{
return new Node(val, head);
}
Node* temp = head;
while(temp->next!=NULL)
{
if(temp->next->data==k)
{
Node* x = new Node(val, temp->next);
temp -> next = x;
break;
}
temp = temp -> next;
}
return head;
}
int main()
{
vector<int> a = {2,5,6,8};
Node* head = convertarraytoLL(a);
int val = 21;
int k = 5;
cout<<lengthofLL(head)<<endl;
head = insertbeforevaluek(head,val,k);
cout<<lengthofLL(head)<<endl;
//int val = 16;
//int k =4;
// int val=8;
// cout<<lengthofLL(head)<<endl;
// head = deletelementequaltoval(head,val);
// cout<<lengthofLL(head)<<endl;
// Node* temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp = temp->next;
// }
// Node x = Node(arr[2],nullptr);
// cout<<x.data<<" "<<x.next<<endl;
// Node* y = new Node(arr[0], nullptr);
// cout<<y->data<<" "<<y->next<<endl;
return 0;
}
Linked List Codes

#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;

public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}

public:
Node(int data2)
{
data = data2;
next = nullptr;
}
};

Node* convertarraytoLL(vector<int> &arr)


{
Node* head = new Node(arr[0]);
Node* mover = head;
for(int i=1;i<arr.size();i++)
{
Node* temp = new Node(arr[i]);
mover->next = temp;
mover = temp;
}
return head;
}

int lengthofLL(Node* head)


{
int count=0;
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
count++;
}
cout<<endl;
return count;
}

int checkIfpresent(Node* head, int val)


{
Node* temp = head;
while(temp!=NULL)
{
if(temp->data==val)
{
return 1;
}
temp = temp->next;
}
return 0;
}

Node* deletehead(Node* head)


{
Node* temp = head;
head = head->next;
free(temp);
//delete temp;
return head;
}

Node* deletetail(Node* head)


{
if(head==NULL||head->next==NULL)
{
return NULL;
}
Node* temp = head;
while(temp->next->next!=NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next = nullptr;
return head;
}
Node* deletektheelement(Node* head, int k)
{
if(head==NULL||head->next==NULL)
{
return NULL;
}
if(k==1)
{
Node* temp = head;
head = head-> next;
free(temp);
return head;
}
int count = 0;
Node* temp = head;
Node* prev = NULL;
while(temp!=NULL)
{
count++;
if(count==k)
{
prev->next = prev->next->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;

Node* deletelementequaltoval(Node* head, int val)


{
if(head==NULL)
{
return NULL;
}
if(head->data==val)
{
Node* temp = head;
head = head-> next;
free(temp);
return head;
}
Node* temp = head;
Node* prev = NULL;
while(temp!=NULL)
{
if(temp->data==val)
{
prev->next = prev->next->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;

Node* newhead(Node* head, int val)


{
Node* temp = new Node(val,head);
return temp;
}
Node* inserttail(Node* head, int val)
{
if(head==NULL)
{
return new Node(val,head);
}
Node* temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
Node* newNode = new Node(val);
temp->next = newNode;
return head;
}

Node* insertatkthposition(Node* head, int val, int k)


{
if(head==NULL)
{
if(k==1)
{
return new Node(val);
}
else
{
return NULL;
}
}
if(k==1)
{
Node* temp = new Node(val,head);
return temp;
}
int count = 0;
Node* temp = head;
while(temp!=NULL)
{
count++;
if(count==k-1)
{
Node* x = new Node(val,temp->next);
temp->next = x;
break;
}
temp = temp->next;
}
return head;
}

Node* insertbeforevaluek(Node* head, int val, int k)


{
if(head==NULL)
{
return NULL;
}
if(head->data==k)
{
return new Node(val, head);
}
Node* temp = head;
while(temp->next!=NULL)
{
if(temp->next->data==k)
{
Node* x = new Node(val, temp->next);
temp -> next = x;
break;
}
temp = temp -> next;
}
return head;
}
int main()
{
vector<int> a = {2,5,6,8};
Node* head = convertarraytoLL(a);
int val = 21;
int k = 5;
cout<<lengthofLL(head)<<endl;
head = insertbeforevaluek(head,val,k);
cout<<lengthofLL(head)<<endl;
//int val = 16;
//int k =4;
// int val=8;
// cout<<lengthofLL(head)<<endl;
// head = deletelementequaltoval(head,val);
// cout<<lengthofLL(head)<<endl;
// Node* temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp = temp->next;
// }
// Node x = Node(arr[2],nullptr);
// cout<<x.data<<" "<<x.next<<endl;
// Node* y = new Node(arr[0], nullptr);
// cout<<y->data<<" "<<y->next<<endl;
return 0;
}

//Doubly Linked List


#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* back;
public:
Node(int data1, Node* next1, Node* back1)
{
data = data1;
next = next1;
back = back1;
}
Node(int data1)
{
data = data1;
next = nullptr;
back = nullptr;
}
};

Node* convertarraytoDoublyLinkedList(vector<int> &a)


{
Node* head = new Node(a[0]);
Node* prev = head;
for(int i = 1; i<a.size();i++)
{
Node* temp = new Node(a[i], nullptr, prev);
prev->next = temp;
prev = temp;
}
return head;
}
void print(Node* head)
{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
Node* deleteheadofDLL(Node* head)
{
if(head==NULL||head->next==NULL)
{
return NULL;
}
Node* prev = head;
head = head->next;
head->back=nullptr;
prev->next=nullptr;
free(prev);
return head;
}

Node* deletetailofDLL(Node* head)


{
if(head==NULL||head->next==NULL)
{
return NULL;
}
Node* tail = head;
while(tail->next!=NULL)
{
tail=tail->next;
}
Node* newtail = tail->back;
newtail->next=nullptr;
tail->back=nullptr;
delete tail;
return head;
}

int lengthofDLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}
Node* deletekthekthElementofDLL(Node* head, int k)
{
if(head==NULL)
{
return NULL;
}
if(k>lengthofDLL(head))
{
return head;
}
Node* kNode = head;
int count = 1;
while(kNode!=NULL)
{
if(count==k)
{
break;
}
count++;
kNode = kNode -> next;
}
Node* prev = kNode -> back;
Node* front = kNode -> next;
if(prev==NULL&&front==NULL)
{
return NULL;
}
// else if(k>lengthofDLL(head))
// {
// cout<<k<<" "<<lengthofDLL(head)<<endl;
// return head; //k is larger than the length of linkedlist
// }
else if(prev==NULL)
{
return deleteheadofDLL(head);
}
else if(front==NULL)
{
return deletetailofDLL(head);
}
prev->next=front;
front->back=prev;
kNode->next=nullptr;
kNode->back=nullptr;
delete kNode;
return head;
}

Node* deleteNodeofDLLequaltok(Node* head, int k)


{
if(head==NULL)
{
return NULL;
}
Node* temp = head;
while(temp!=NULL)
{
if(temp->data == k)
{
break;
}
temp = temp->next;
}
Node* prev = temp->back;
Node* front = temp->next;

if(prev==NULL&&front==NULL)
{
return NULL;
}
else if(prev==NULL)
{
return deleteheadofDLL(head);
}
else if(front==NULL)
{
return deletetailofDLL(head);
}
prev->next = front;
front->back = prev;
temp->next = nullptr;
temp->back = nullptr;
delete temp;
return head;
}

Node* insertBeforeHead(Node* prevhead,int val)


{
Node* newHead = new Node(val, prevhead, nullptr);
prevhead->back=newHead;
return newHead;
}

Node* insertBeforeTail(Node* head, int val)


{
if(head==NULL)
{
return new Node(val,head,nullptr);
}
if(head->next==NULL)
{
return insertBeforeHead(head, val);
}
Node* tail = head;
while(tail->next!=NULL)
{
tail = tail->next;
}
Node* prev = tail->back;
Node* element = new Node(val, tail,prev);
tail->back=element;
prev->next=element;
return head;
}

Node* insertbeforeKthelementinDLL(Node* head, int val, int k)


{
if(k==1)
{
return insertBeforeHead(head,val);
}
if(k>lengthofDLL(head))
{
return head;
}
Node* temp = head;
int count = 1;
while(temp!=NULL)
{
if(count==k)
{
break;
}
count++;
temp=temp-> next;
}
Node* prev = temp->back;
Node* newNode = new Node(val,temp,prev);
prev->next = newNode;
temp->back = newNode;
return head;
}

Node* insertbeforeNodeequaltokinDLL(Node* head, int val, int k)


{
if(head->data==k)
{
return insertBeforeHead(head, val);
}
Node* temp = head;
while(temp!=NULL)
{
if(temp->data==k)
{
break;
}
temp = temp->next;
}
Node* prev = temp->back;
Node* newNode = new Node(val, temp, prev);
prev->next=newNode;
temp->back=newNode;
return head;
}

Node* reverseDLL(Node* head)


{
if(head==NULL||head->next==NULL)
{
return head;
}
Node* last = NULL;
Node* current = head;
while(current!=NULL)
{
last = current->back;
current->back=current->next;
current->next=last;
current = current->back;
}
return last->back;
}

int main()
{
vector<int> a = {5,6,1,2,3,4};
vector<int> b = {1};
vector<int> c = {};
Node* head = convertarraytoDoublyLinkedList(a);
print(head);
//cout<<lengthofDLL(head)<<endl;
head = insertbeforeKthelementinDLL(head,10,5);
print(head);
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}

//sum of two numbers


#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* back;
public:
Node(int data1, Node* next1, Node* back1)
{
data = data1;
next = next1;
back = back1;
}
Node(int data1)
{
data = data1;
next = nullptr;
back = nullptr;
}
};

Node* convertarraytoDoublyLinkedList(vector<int> &a)


{
Node* head = new Node(a[0]);
Node* prev = head;
for(int i = 1; i<a.size();i++)
{
Node* temp = new Node(a[i], nullptr, prev);
prev->next = temp;
prev = temp;
}
return head;
}
void print(Node* head)
{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}

int lengthofDLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}

Node* sumoftwonumbers(Node* head1, Node* head2)


{
Node* temp1 = head1;
Node* temp2 = head2;
Node* dummyNode = new Node(-1);
int carry=0;
Node* current = dummyNode;
while(temp1!=NULL||temp2!=NULL)
{
int sum = carry;
if(temp1!=NULL)
{
sum = sum+temp1->data;
}
if(temp2!=NULL)
{
sum = sum+temp2->data;
}
Node* newNodedata = new Node(sum%10);
carry = sum/10;
current->next=newNodedata;
current = current->next;
if(temp1!=NULL)
{
temp1=temp1->next;
}
if(temp2!=NULL)
{
temp2=temp2->next;
}
}
if(carry)
{
Node* newNodedata = new Node(carry);
current->next = newNodedata;
}
return dummyNode->next;
}

int main()
{
vector<int> a = {3,5};
vector<int> b = {4,5,9,9};
Node* head1 = convertarraytoDoublyLinkedList(a);
Node* head2 = convertarraytoDoublyLinkedList(b);
print(head1);
print(head2);
//cout<<lengthofDLL(head)<<endl;
Node* head3 = sumoftwonumbers(head1,head2);
print(head3);
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}

// Odd Even Linked List


#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* back;
public:
Node(int data1, Node* next1, Node* back1)
{
data = data1;
next = next1;
back = back1;
}
Node(int data1)
{
data = data1;
next = nullptr;
back = nullptr;
}
};
Node* convertarraytoDoublyLinkedList(vector<int> &a)
{
Node* head = new Node(a[0]);
Node* prev = head;
for(int i = 1; i<a.size();i++)
{
Node* temp = new Node(a[i], nullptr, prev);
prev->next = temp;
prev = temp;
}
return head;
}

void print(Node* head)


{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}

int lengthofDLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}

Node* oddEvenList(Node* head)


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

Node* odd = head;


Node* even = head->next;
Node* evenHead = head->next;

while(even!=NULL && even->next!=NULL){


odd->next = odd->next->next;
even->next = even->next->next;

odd = odd->next;
even = even->next;
}
odd->next = evenHead;
return head;
}

int main()
{
vector<int> a = {1,2,3,4,5,6,7};
Node* head1 = convertarraytoDoublyLinkedList(a);
print(head1);
Node* head = oddEvenList(head1);
print(head);
//cout<<lengthofDLL(head)<<endl;
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}

//reverse a linkedlist
#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};

Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}
void display(Node* head)
{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}

int lengthofLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}
Node* llreverse(Node* head)
{
stack<int> st;
Node* temp = head;
while(temp!=NULL)
{
st.push(temp->data);
temp = temp->next;
}
temp = head;
while(temp!=NULL)
{
temp->data = st.top();
st.pop();
temp=temp->next;
}
return head;
}

Node* reversellapproach2(Node* head)


{
Node* temp =head;
Node* prev = NULL;
while(temp!=NULL)
{
Node* front = temp->next;
temp->next=prev;
prev=temp;
temp=front;
}
return prev;
}

Node* reverseLinkedList(Node* head) {


// Base case:
// If the linked list is empty or has only one node,
// return the head as it is already reversed.
if (head == NULL || head->next == NULL) {
return head;
}

// Recursive step:
// Reverse the linked list starting
// from the second node (head->next).
Node* newHead = reverseLinkedList(head->next);

// Save a reference to the node following


// the current 'head' node.
Node* front = head->next;
// Make the 'front' node point to the current
// 'head' node in the reversed order.
front->next = head;

// Break the link from the current 'head' node


// to the 'front' node to avoid cycles.
head->next = NULL;
// Return the 'newHead,' which is the new
// head of the reversed linked list.
return newHead;
}

int main()
{
vector<int> a = {9,0,1,3,6};
Node* head = arraytoLL(a);
display(head);
head = reverseLinkedList(head);
display(head);
}

//check if a linkedlist is palindrome or not


#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};

Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}

void display(Node* head)


{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}

int lengthofLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}

bool bruteforcepalindrome(Node* head)


{
Node* temp = head;
stack<int> st;
while(temp!=NULL)
{
st.push(temp->data);
temp=temp->next;
}
temp=head;
while(temp!=NULL)
{
if(temp->data!=st.top())
{
return false;
}
st.pop();
temp=temp->next;
}
return true;
}

Node* reversell(Node* head)


{
if(head==NULL||head->next==NULL)
{
return head;
}
Node* newhead = reversell(head->next);
Node* front = head->next;
front->next = head;
head->next=NULL;
return newhead;
}

bool optimisedpalindrome(Node* head)


{
Node* slow = head;
Node* fast = head;
while(fast->next!=NULL&&fast->next->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
Node* newhead = reversell(slow->next);
Node* first = head;
Node* second = newhead;
while(second!=NULL)
{
if(first->data!=second->data)
{
reversell(newhead);
return false;
}
first = first->next;
second=second->next;
}
reversell(newhead);
return true;
}

int main()
{
vector<int> a = {1,4,6,6,4,1};
Node* head = arraytoLL(a);
display(head);
cout<<optimisedpalindrome(head);
}

//sort 0 1 2
#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};

Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}

void bruteforcesort(Node* head)


{
Node* temp = head;
int zerocount = 0, onecount = 0, twocount=0;
while(temp!=NULL)
{
if(temp->data==0)
zerocount++;
else if(temp->data==1)
onecount++;
else if(temp->data==2)
twocount++;
temp = temp->next;
}
Node* temp1 = head;
while(temp1!=NULL)
{
if(zerocount>0)
{
temp1->data=0;
zerocount--;
}
else if(onecount>0)
{
temp1->data=1;
onecount--;
}
else if(twocount>0)
{
temp1->data=2;
twocount--;
}
temp1 = temp1->next;
}
}

Node* optimisedsort(Node* head) {


// If the list is empty or has only one node, it's already sorted
if (head == NULL || head->next == NULL) {
return head;
}
// Create dummy heads for 0s, 1s, and 2s
Node* zeroHead = new Node(-1);
Node* oneHead = new Node(-1);
Node* twoHead = new Node(-1);
// Pointers to the last node of the respective lists
Node* zero = zeroHead;
Node* one = oneHead;
Node* two = twoHead;
// Traverse the input list
Node* temp = head;
while (temp != NULL) {
if (temp->data == 0) {
zero->next = temp;
zero = zero->next;
} else if (temp->data == 1) {
one->next = temp;
one = one->next;
} else { // temp->data == 2
two->next = temp;
two = two->next;
}
temp = temp->next;
}
// Connect the 0s list to the 1s list (if exists), otherwise to 2s
zero->next = (oneHead->next != NULL) ? oneHead->next : twoHead->next;
// Connect the 1s list to the 2s list
one->next = twoHead->next;
// End the 2s list
two->next = NULL;
// The new head is the next node after zeroHead
Node* sortedHead = zeroHead->next;
// Free up the dummy nodes to avoid memory leaks
delete zeroHead;
delete oneHead;
delete twoHead;
return sortedHead;
}

void display(Node* head)


{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int lengthofLL(Node* head)
{
Node* temp = head;
int count = 0;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}

Node* deletenodefrombackbruteforce(Node* head, int k)


{
if(head==NULL||head->next==NULL)
{
return NULL;
}
int len = lengthofLL(head);
if((len-k+1)==1)
{
Node* temp = head;
head=head->next;
delete(temp);
return head;
}
Node* temp = head;
Node* prev = NULL;
int count = 0;
while(temp!=NULL)
{
count++;
if(count == (len-k+1))
{
prev->next = prev->next->next;
delete(temp);
break;
}
prev = temp;
temp=temp->next;
}
return head;
}

Node* DeleteNthNodefromEnd(Node* head, int N) {


// Create two pointers, fastp and slowp
Node* fastp = head;
Node* slowp = head;
// Move the fastp pointer N nodes ahead
for (int i = 0; i < N; i++)
fastp = fastp->next;

// If fastp becomes NULL,


// the Nth node from the end is the head
if (fastp == NULL)
return head->next;

// Move both pointers until fastp reaches the end


while (fastp->next != NULL) {
fastp = fastp->next;
slowp = slowp->next;
}

// Delete the Nth node from the end


Node* delNode = slowp->next;
slowp->next = slowp->next->next;
delete delNode;
return head;
}

Node* llreverse(Node* head)


{
stack<int> st;
Node* temp = head;
while(temp!=NULL)
{
st.push(temp->data);
temp = temp->next;
}
temp = head;
while(temp!=NULL)
{
temp->data = st.top();
st.pop();
temp=temp->next;
}
return head;
}

Node* reversellapproach2(Node* head)


{
Node* temp =head;
Node* prev = NULL;
while(temp!=NULL)
{
Node* front = temp->next;
temp->next=prev;
prev=temp;
temp=front;
}
return prev;
}

Node* reverseLinkedList(Node* head)


{
if (head == NULL || head->next == NULL) {
return head;
}
Node* newHead = reverseLinkedList(head->next);
Node* front = head->next;
front->next = head;
head->next = NULL;
return newHead;
}

int main()
{
vector<int> a = {9,8,7,6,5};
Node* head = arraytoLL(a);
display(head);
head = reverseLinkedList(head);
display(head);
}

//sliding window first negative in each sub array

#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a = {-1,4,4,5,-2,4,9};
int i=0,j=0;
list<int>l;
while(j<a.size())
{
if(a[j]<0)
{
l.push_back(a[j]);
}
if(j-i+1<3)
{
j++;
}
else
{
cout<<l.front()<<" ";
if(a[i]<0)
{
l.pop_front();
}
i++;
j++;
}
}
}

// hashing group anagram


#include <bits/stdc++.h>
using namespace std;
void groupanagram(vector<string> a)
{
unordered_map<string, vector<string>> mp;
for(int i=0;i<a.size();i++)
{
string key = a[i];
sort(key.begin(),key.end());
mp[key].push_back(a[i]);
}
for(auto iter = mp.begin();iter!=mp.end();iter++)
{
for(int i=0;i<iter->second.size();i++)
{
cout<<iter->second[i]<<" ";
}
}
}
void display(vector<string> a)
{
for(auto val:a)
{
cout<<val<<" ";
}
cout<<endl;
}

int main()
{
vector<string> a = {"ate","bat","cat","tae","act","eat"};
display(a);
groupanagram(a);
}

//relative sort
#include <bits/stdc++.h>
using namespace std;
void relativesort(vector<int> a, vector<int> b)
{
map<int, int> mp;
for(int i=0;i<a.size();i++)
{
mp[a[i]]++;
}
for(int i=0;i<b.size();i++)
{
if(mp.find(b[i])!=mp.end())
{
auto iter = mp.find(b[i]);
int count = iter->second;
while(count!=0)
{
cout<<iter->first<<" ";
count--;
}
mp.erase(b[i]);
}
}
for(auto iter = mp.begin();iter!=mp.end();iter++)
{
int count = iter->second;
int val = iter->first;
while(count!=0)
{
cout<<iter->first<<" ";
count--;
}
}
}
void customsort(string s, string order)
{
map<char, int> mp;
for(int i=0;i<s.size();i++)
{
mp[s[i]]++;
}
for(int i=0;i<order.size();i++)
{
if(mp.find(order[i])!=mp.end())
{
auto iter = mp.find(order[i]);
int count = iter->second;
while(count!=0)
{
cout<<iter->first;
count--;
}
mp.erase(order[i]);
}
}
for(auto iter = mp.begin();iter!=mp.end();iter++)
{
int count = iter->second;
int val = iter->first;
while(count!=0)
{
cout<<iter->first;
count--;
}
}
}
void display(vector<int> a)
{
for(auto val:a)
{
cout<<val<<" ";
}
cout<<endl;
}

int main()
{
vector<int> a = {2,3,6,1,3,2,4,8,6,7,9,2};
vector<int> b = {2,1,4,3,9};
display(a);
display(b);
relativesort(a,b);
string s = "adbhcadh";
string order = "cba";
cout<<endl<<s<<endl<<order<<endl;
customsort(s,order);
}

//add 1 to a number stored in a linked list


#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};

Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}

void display(Node* head)


{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}

int lengthofLL(Node* head)


{
Node* temp = head;
int count = 0;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}

Node* reverseLinkedList(Node* head)


{
if (head == NULL || head->next == NULL) {
return head;
}
Node* newhead = reverseLinkedList(head->next);
Node* front = head->next;
front->next = head;
head->next = NULL;
return newhead;
}

Node* add1toanumber(Node* head)


{
Node* newhead = reverseLinkedList(head);
Node* temp = newhead;
int carry = 1;
while(temp!=NULL)
{
temp->data = temp->data+carry;
if(temp->data<10)
{
carry = 0;
break;
}
else
{
temp->data=0;
carry=1;
}
temp = temp->next;
}
if(carry==1)
{
Node* newNode = new Node(1);
head = reverseLinkedList(newhead);
newNode->next = head;
return newNode;
}
head = reverseLinkedList(newhead);
return head;
}

int helper(Node* head)


{
Node* temp = head;
if(temp==NULL)
{
return 1;
}
int carry = helper(temp->next);
temp->data=temp->data+carry;
if(temp->data<10)
{
return 0;
}
temp->data=0;
return 1;
}

Node* recursiveadd1toanumber(Node* head)


{
int carry=helper(head);
if(carry==1)
{
Node* newNode = new Node(1);
newNode->next = head;
return newNode;
}
return head;
}
int main()
{
vector<int> a = {9,9,9,9};
vector<int> b = {1,5,9};
vector<int> c = {9};
Node* head = arraytoLL(a);
Node* head1 = arraytoLL(b);
Node* head2 = arraytoLL(c);
display(head);
head = recursiveadd1toanumber(head);
display(head);
display(head1);
head1 = recursiveadd1toanumber(head1);
display(head1);
display(head2);
head2 = recursiveadd1toanumber(head2);
display(head2);
}

You might also like