Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

// Reverse the Linked List

//
// Iterative approach
// Approach :- 3 node create ki_ye prev, curr, forward. ----> curr k aa_ge k nodes ko forward mai
store kr di_ya(jis_se wo lost na ho jaa_ye)-->
// {i.e.forward shift kr di_ya} and curr->next mai prev ko store kr diy_a and
[prev=curr and curr=forward]----> curr and prev ko shift kr_ke update kr di_ya
//
//
// Recursive Approach
// Ham 1st node ko solve kr de_nge and bak_i recursion kr deg_a--->base case-->jb iterate krk_e
curr-> NULL ko point kre & prev-> last node ko
// head node[1st k curr ka next] k aa.ge ka part lost na ho isl.ie us.se forward pointer mai store kr
den_ge------>recursive function mai curr & prev ko shift kr den_ge i.e. curr->forward &
prev=curr------>curr->next= prev
//
// Best Approach-> base case da_al di_ya then ------>head node k aa_ge k sab_hi nodes ko
recursive function mai pass kr di_ya-------> hame recursion ki help sai un_ke reverse ka head mil
gya
//
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// // ---------------------------------------------[ REVERSE -> BEST RECURSIVE
APPROACH ]-----------------------------------
// //
// // Approach-> base case da_al di_ya then ------>head node k aa_ge k sab_hi nodes ko
recursive function mai pass kr di_ya-------> hame recursion ki help sai un_ke reverse ka head mil
gya
// // Node* reverse1(Node*head)
// // {
// // //Base case------> Jah_an pai terminate ho_ga
// // if(head==NULL||head->next==NULL)
// // {
// // return head;
// // } // Head NULL
// // Node* Chh_otaHead=reverse1(head->next); // _______ |
________|---------------->head->next->next
// // head->next->next=head; //-----------------------> / 18 / -/--X-->/ 17 /
-/|--|
// // head->next=NULL; //-----------------------> -------- -------- |
// // print(Chh_otaHead); // ^ | 1
case that we solve
// // return Chh_otaHead; // |------<--------<------|
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(n)
// //
// //
// // -----------------------------------------[ REVERSE -> ITERATIVE
APPROACH ]--------------------------------------
// // Node* reverse(Node* &head)
// // {
// // if(head==NULL|| head->next==NULL)
// // {
// // return head;
// // }
// // Node* prev=NULL;
// // Node* curr=head;
// // Node* forward=NULL;
// // while(curr!=NULL)
// // {
// // forward=curr->next;
// // curr->next=prev;
// // prev=curr;
// // curr=forward;
// // }
// // print(prev);
// // return prev;
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(1)
// //
// // -------------------------------------------[ REVERSE -> RECURSIVE
APPROACH ]-------------------------------------
// // Node* RecReverse(Node* &head, Node* curr, Node* prev)
// // {
// // if(curr==NULL)
// // {
// // head=prev;
// // return head;
// // }
// // Node* forward=curr->next;
// // RecReverse(head,forward,curr);
// // cout<<curr->data<<" ";
// // curr->next=prev;
// // return head;
// // }
// // //
// // Node* reverse(Node* &head)
// // {
// // Node* prev=NULL;
// // Node* curr=head;
// // RecReverse(head,curr,prev);
// // return head;
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(1)
// //------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
// //
// void InsertAtBegin(Node* &head, int data) //--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data) //--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void InsertAnyPos(Node* &tail,Node* &head,int position,int data)
// //
// {
// Node* temp=head;
// int cnt=1;
// //
// //
// while(cnt<position-1)
// {
// temp=temp->next;
// cnt++;
// }
// // Inserting at Start
// if(position==1)
// {
// InsertAtBegin(head,data);//------------------------------->head update ho jaa_ye_ga
// return;
// }
// //
// // Inserting at Last
// if(temp->next==NULL)
// {
// InsertAtTail(tail,data);//------------------------------->tail update ho jaa_ye_ga
// return;
// }
// // Insert at any position
// // Creating a new node
// Node *node_to_insert=new Node(data);
// node_to_insert->next=temp->next;
// temp->next=node_to_insert;
// }
// //------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------
// //
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(15);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,14); InsertAtBegin(head,13); InsertAtBegin(head,12); print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,16); InsertAtTail(tail,17); InsertAtTail(tail,18);
// print(node1); cout<<endl;
// cout<<"The value in the Linked List are : "; print(head); cout<<endl;
// //
// // Approach :- 3 node create ki_ye prev, curr, forward. ----> curr k aa_ge k nodes ko forward
mai store kr di_ya-->forward shift kr di_ya and curr->next mai prev ko store kr diy_a and
[prev=curr and curr=forward] curr and prev ko shift kr_ke update kr di_ya
// cout<<"Reversing the Linked List using Iterative Approach: "<<endl;
// reverse1(head);
// return 0;
// }

// H.W.->Reverse the Doubly Linked List

// Finding middle of List


// Given the head node of the singly linked list, return a pointer pointing to the middle of the linked
list.
// If there are an odd number of elements, return the middle element if there are even elements
return the one which is farther from the head node.
// For example, let the linked list be 1 -> 2 -> 3 ->4 -> NULL
// Since the number of elements in this linked list is 4 so we have 2 middle elements, i.e. 2 and 3,
but we return 3 as it is farther from the head node, i.e. 1.
//
//
// // Approach-> length of linked list nd ki ham_ne then usk_a half ans mai store kr diy_a -------->
head ko temp mai store kr di_ya and count integer variable bna_ya--------> ek loop cha_la_ya
[count<ans] ---->temp=tamp->next kr diy_a-------> temp->data is showing the middle node
//
// int getLength(Node* head)
// {
// if(head==NULL)
// {
// return 1;
// }
// int count=0;
// while(head!=NULL)
// {
// count++;
// head=head->next;
// }
// return count;
// }
// //
// Node* getMiddleNode(Node* head)
// {
// int len=getLength(head);
// int ans=len/2;
// Node* temp=head;
// int count=0;
// while(count<ans)
// {
// temp=temp->next;
// count++;
// }
// return temp;
// }
//
//
//
// // Approach-> 2 pointers fast and slow are created---->fast move 2 steps and slow move 1 step
until fast!=NULL------->slow->data is the data of middle node
//
fi
// Node* MiddleNode(Node* head)
// {
// if(head==NULL || head->next==NULL)
// {
// return head;
// }
// // 2 nodes
// if(head->next->next==NULL)
// {
// return head;
// }
// Node* slow=head;
// Node* fast=head->next;
// while(fast!=NULL)
// {
// fast=fast->next;
// if(fast!=NULL)
// {
// fast=fast->next;
// }
// slow=slow->next;
// }
// return slow;
// }
//
//
//
//
//
//

// Reverse the Linked List in K group


// Time Complexity :- O(n) Space Complexity :- O(n)
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// //
// Node* kReverse(Node* &head, int k)
// {
// // Base Case
// if(head==NULL)
// {
// return NULL;
// }
//
// // Step-1 : reverse 1st k nodes
// int count=0;
// Node* nex=NULL;
// Node* curr=head;
// Node* prev=NULL;
// while(curr!=NULL && count<k)
// {
// nex=curr->next;
// curr->next=prev;
// prev=curr;
// curr=nex;
// count++;
// }
// // |--<----<---<---<----|
// // ___curr_|_ ____nex___ | // ____prev___ prev---| |------->head->next
// // / 12 / - /---->| / 13 / - /-| // / NULL / - /----| 13->12->Null--------->Remaining part
ko-->curr and nex point kr rhe hai jis_se wo lost a ho------->Recursion solve kre_ga
// // ---------- | ---------- // ----------- | Recursion-> Remaining part ko reverse
kr_ke ha_me reverse list ka head dai de_ga------> & us head ko ha_me head->next mai lga_na hai
// // head | prev | Reverse List k head ko previous point kr rha
hai------> return prev kr di_ya
// // |-------------------------------------------|
// //
// // Step-2 Recursion----------->1st case k ala_wa sb_ko solve kre_ga
// if(nex!=NULL)
// {
// head->next=kReverse(nex,k);
// }
// //
// // return head of reversed list
// print(prev);
// return prev;
// ----------------------------------------------------------Veri cation
Below----------------------------------------------

// Check the given Linked List is circular or not


// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL:
// }
// };
// bool isCircular(Node* head)
// {
// if(head==NULL)
// {
// cout<<"List is empty";
// return false;
// }
// Node* temp=head->next;
// while(temp!=NULL&& temp!=head)
// if(temp==head)
// {
// cout<<"List is circular";
// return true;
// }
fi
// return false;
// };

// --------------------------------------------------------------------Veri cation of above


approach-------------------------------------------------------------
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// //
// //
----------------------------------------------------------------------------------------------------------------
---------------------------------------------------
// int getLength(Node* head)
// {
// if(head==NULL)
// {
// return 1;
// }
// int count=0;
// while(head!=NULL)
// {
// count++;
// head=head->next;
// }
// return count;
// }
// Node* getMiddleNode(Node* head)
// {
// int len=getLength(head);
// int ans=len/2;
// //
// Node* temp=head;
// int count=0;
// while(count<ans)
// {
// temp=temp->next;
// count++;
// }
// cout<<temp->data;
fi
// return temp;
// }
// Node* MiddleNode(Node* head)
// {
// if(head==NULL || head->next==NULL)
// {
// return head;
// }
// // 2 nodes
// if(head->next->next==NULL)
// {
// return head;
// }
// Node* slow=head;
// Node* fast=head->next;
// while(fast!=NULL)
// {
// fast=fast->next;
// if(fast!=NULL)
// {
// fast=fast->next;
// }
// slow=slow->next;
// }
// cout<<slow->data;
// return slow;
// }
// //
----------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------
// Node* kReverse(Node* &head, int k)
// {
//
// // Base Case
// if(head==NULL)
// {
// return NULL;
// }
// int count=0;
// Node* nex=NULL;
// Node* curr=head;
// Node* prev=NULL;
// while(curr!=NULL && count<k)
// {
// nex=curr->next;
// curr->next=prev;
// prev=curr;
// curr=nex;
// count++;
// }
// // prev curr
// // ____curr__ ____nex___ // ____prev__
// // / 12 / - /---->| / 13 / - /-- // / NULL / - /---|
// // --------- | --------- // --------- |
// // | |
// // |----------------------------------------|
// //
// // Step-2 Recursion----------->1st case k ala_wa sb_ko solve kre_ga
// if(nex!=NULL)
// {
// head->next=kReverse(nex,k);
// }
// //
// // return head of reversed list
// print(prev);
// return prev;
// }
// //------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
// //
// void InsertAtBegin(Node* &head, int data) //--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data) //--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void InsertAnyPos(Node* &tail,Node* &head,int position,int data)
// //
// {
// Node* temp=head;
// int cnt=1;
// //
// //
// while(cnt<position-1)
// {
// temp=temp->next;
// cnt++;
// }
// // Inserting at Start
// if(position==1)
// {
// InsertAtBegin(head,data);//------------------------------->head update ho jaa_ye_ga
// return;
// }
// //
// // Inserting at Last
// if(temp->next==NULL)
// {
// InsertAtTail(tail,data);//------------------------------->tail update ho jaa_ye_ga
// return;
// }
// // Insert at any position
// // Creating a new node
// Node *node_to_insert=new Node(data);
// node_to_insert->next=temp->next;
// temp->next=node_to_insert;
// }
// //------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------
// //
// bool isCircular(Node* head)
// {
// if(head==NULL)
// {
// cout<<"List is empty";
// return false;
// }
// Node* temp=head->next;
// while(temp!=NULL&& temp!=head)
// {
// temp=temp->next;
// }
// if(temp==head)
// {
// cout<<"List is circular";
// return true;
// }
// cout<<"List is not circular";
// return false;
// };
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(15);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,14); InsertAtBegin(head,13); InsertAtBegin(head,12); print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,16); InsertAtTail(tail,17); InsertAtTail(tail,18);
// print(node1); cout<<endl;
// cout<<"The value in the Linked List are : "; print(head); cout<<endl;
// // cout<<getLength(head)<<endl;
// // cout<<getMiddleNode(head)->data<<endl;
// // cout<<MiddleNode(head)->data<<endl;
// cout<<isCircular(tail);
// // kReverse(head,2); cout<<endl;
// return 0;
// }
// Reverse the Linked List
//
// Iterative approach
// Approach :- 3 node create ki_ye prev, curr, forward. ----> curr k aa_ge k nodes ko forward mai
store kr di_ya(jis_se wo lost na ho jaa_ye)-->
// {i.e.forward shift kr di_ya} and curr->next mai prev ko store kr diy_a and
[prev=curr and curr=forward]----> curr and prev ko shift kr_ke update kr di_ya
//
//
// Recursive Approach
// Ham 1st node ko solve kr de_nge and bak_i recursion kr deg_a--->base case-->jb iterate krk_e
curr-> NULL ko point kre & prev-> last node ko
// head node[1st k curr ka next] k aa.ge ka part lost na ho isl.ie us.se forward pointer mai store kr
den_ge------>recursive function mai curr & prev ko shift kr den_ge i.e. curr->forward &
prev=curr------>curr->next= prev
//
// Best Approach-> base case da_al di_ya then ------>head node k aa_ge k sab_hi nodes ko
recursive function mai pass kr di_ya-------> hame recursion ki help sai un_ke reverse ka head mil
gya
//
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// // ---------------------------------------------[ REVERSE -> BEST RECURSIVE
APPROACH ]-----------------------------------
// //
// // Approach-> base case da_al di_ya then ------>head node k aa_ge k sab_hi nodes ko
recursive function mai pass kr di_ya-------> hame recursion ki help sai un_ke reverse ka head mil
gya
// // Node* reverse1(Node*head)
// // {
// // //Base case------> Jah_an pai terminate ho_ga
// // if(head==NULL||head->next==NULL)
// // {
// // return head;
// // } // Head NULL
// // Node* Chh_otaHead=reverse1(head->next); // _______ |
________|---------------->head->next->next
// // head->next->next=head; //-----------------------> / 18 / -/--X-->/ 17 /
-/|--|
// // head->next=NULL; //-----------------------> -------- -------- |
// // print(Chh_otaHead); // ^ | 1
case that we solve
// // return Chh_otaHead; // |------<--------<------|
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(n)
// //
// //
// // -----------------------------------------[ REVERSE -> ITERATIVE
APPROACH ]--------------------------------------
// // Node* reverse(Node* &head)
// // {
// // if(head==NULL|| head->next==NULL)
// // {
// // return head;
// // }
// // Node* prev=NULL;
// // Node* curr=head;
// // Node* forward=NULL;
// // while(curr!=NULL)
// // {
// // forward=curr->next;
// // curr->next=prev;
// // prev=curr;
// // curr=forward;
// // }
// // print(prev);
// // return prev;
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(1)
// //
// // -------------------------------------------[ REVERSE -> RECURSIVE
APPROACH ]-------------------------------------
// // Node* RecReverse(Node* &head, Node* curr, Node* prev)
// // {
// // if(curr==NULL)
// // {
// // head=prev;
// // return head;
// // }
// // Node* forward=curr->next;
// // RecReverse(head,forward,curr);
// // cout<<curr->data<<" ";
// // curr->next=prev;
// // return head;
// // }
// // //
// // Node* reverse(Node* &head)
// // {
// // Node* prev=NULL;
// // Node* curr=head;
// // RecReverse(head,curr,prev);
// // return head;
// // }
// // TIME COMPLEXITY->O(n) SPACE COMPLEXITY->O(1)
// //------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
// //
// void InsertAtBegin(Node* &head, int data) //--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data) //--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void InsertAnyPos(Node* &tail,Node* &head,int position,int data)
// //
// {
// Node* temp=head;
// int cnt=1;
// //
// //
// while(cnt<position-1)
// {
// temp=temp->next;
// cnt++;
// }
// // Inserting at Start
// if(position==1)
// {
// InsertAtBegin(head,data);//------------------------------->head update ho jaa_ye_ga
// return;
// }
// //
// // Inserting at Last
// if(temp->next==NULL)
// {
// InsertAtTail(tail,data);//------------------------------->tail update ho jaa_ye_ga
// return;
// }
// // Insert at any position
// // Creating a new node
// Node *node_to_insert=new Node(data);
// node_to_insert->next=temp->next;
// temp->next=node_to_insert;
// }
// //------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------
// //
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(15);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,14); InsertAtBegin(head,13); InsertAtBegin(head,12); print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,16); InsertAtTail(tail,17); InsertAtTail(tail,18);
// print(node1); cout<<endl;
// cout<<"The value in the Linked List are : "; print(head); cout<<endl;
// //
// // Approach :- 3 node create ki_ye prev, curr, forward. ----> curr k aa_ge k nodes ko forward
mai store kr di_ya-->forward shift kr di_ya and curr->next mai prev ko store kr diy_a and
[prev=curr and curr=forward] curr and prev ko shift kr_ke update kr di_ya
// cout<<"Reversing the Linked List using Iterative Approach: "<<endl;
// reverse1(head);
// return 0;
// }

// H.W.->Reverse the Doubly Linked List

// Finding middle of List


// Given the head node of the singly linked list, return a pointer pointing to the middle of the linked
list.
// If there are an odd number of elements, return the middle element if there are even elements
return the one which is farther from the head node.
// For example, let the linked list be 1 -> 2 -> 3 ->4 -> NULL
// Since the number of elements in this linked list is 4 so we have 2 middle elements, i.e. 2 and 3,
but we return 3 as it is farther from the head node, i.e. 1.
//
//
// // Approach-> length of linked list nd ki ham_ne then usk_a half ans mai store kr diy_a -------->
head ko temp mai store kr di_ya and count integer variable bna_ya--------> ek loop cha_la_ya
[count<ans] ---->temp=tamp->next kr diy_a-------> temp->data is showing the middle node
//
// int getLength(Node* head)
// {
// if(head==NULL)
// {
// return 1;
// }
// int count=0;
// while(head!=NULL)
// {
// count++;
// head=head->next;
// }
// return count;
// }
// //
// Node* getMiddleNode(Node* head)
// {
// int len=getLength(head);
// int ans=len/2;
// Node* temp=head;
// int count=0;
// while(count<ans)
// {
// temp=temp->next;
// count++;
// }
// return temp;
// }
//
//
//
// // Approach-> 2 pointers fast and slow are created---->fast move 2 steps and slow move 1 step
until fast!=NULL------->slow->data is the data of middle node
//
fi
// Node* MiddleNode(Node* head)
// {
// if(head==NULL || head->next==NULL)
// {
// return head;
// }
// // 2 nodes
// if(head->next->next==NULL)
// {
// return head;
// }
// Node* slow=head;
// Node* fast=head->next;
// while(fast!=NULL)
// {
// fast=fast->next;
// if(fast!=NULL)
// {
// fast=fast->next;
// }
// slow=slow->next;
// }
// return slow;
// }
//
//
//
//
//
//

// Reverse the Linked List in K group


// Time Complexity :- O(n) Space Complexity :- O(n)
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// //
// Node* kReverse(Node* &head, int k)
// {
// // Base Case
// if(head==NULL)
// {
// return NULL;
// }
//
// // Step-1 : reverse 1st k nodes
// int count=0;
// Node* nex=NULL;
// Node* curr=head;
// Node* prev=NULL;
// while(curr!=NULL && count<k)
// {
// nex=curr->next;
// curr->next=prev;
// prev=curr;
// curr=nex;
// count++;
// }
// // |--<----<---<---<----|
// // ___curr_|_ ____nex___ | // ____prev___ prev---| |------->head->next
// // / 12 / - /---->| / 13 / - /-| // / NULL / - /----| 13->12->Null--------->Remaining part
ko-->curr and nex point kr rhe hai jis_se wo lost a ho------->Recursion solve kre_ga
// // ---------- | ---------- // ----------- | Recursion-> Remaining part ko reverse
kr_ke ha_me reverse list ka head dai de_ga------> & us head ko ha_me head->next mai lga_na hai
// // head | prev | Reverse List k head ko previous point kr rha
hai------> return prev kr di_ya
// // |-------------------------------------------|
// //
// // Step-2 Recursion----------->1st case k ala_wa sb_ko solve kre_ga
// if(nex!=NULL)
// {
// head->next=kReverse(nex,k);
// }
// //
// // return head of reversed list
// print(prev);
// return prev;
// ----------------------------------------------------------Veri cation
Below----------------------------------------------

// Check the given Linked List is circular or not


// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL:
// }
// };
// bool isCircular(Node* head)
// {
// if(head==NULL)
// {
// cout<<"List is empty";
// return false;
// }
// Node* temp=head->next;
// while(temp!=NULL&& temp!=head)
// if(temp==head)
// {
// cout<<"List is circular";
// return true;
// }
fi
// return false;
// };

// --------------------------------------------------------------------Veri cation of above


approach-------------------------------------------------------------
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node* next;
// Node(int data)
// {
// this->data=data;
// this->next=NULL;
// }
// };
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// //
// //
----------------------------------------------------------------------------------------------------------------
---------------------------------------------------
// int getLength(Node* head)
// {
// if(head==NULL)
// {
// return 1;
// }
// int count=0;
// while(head!=NULL)
// {
// count++;
// head=head->next;
// }
// return count;
// }
// Node* getMiddleNode(Node* head)
// {
// int len=getLength(head);
// int ans=len/2;
// //
// Node* temp=head;
// int count=0;
// while(count<ans)
// {
// temp=temp->next;
// count++;
// }
// cout<<temp->data;
fi
// return temp;
// }
// Node* MiddleNode(Node* head)
// {
// if(head==NULL || head->next==NULL)
// {
// return head;
// }
// // 2 nodes
// if(head->next->next==NULL)
// {
// return head;
// }
// Node* slow=head;
// Node* fast=head->next;
// while(fast!=NULL)
// {
// fast=fast->next;
// if(fast!=NULL)
// {
// fast=fast->next;
// }
// slow=slow->next;
// }
// cout<<slow->data;
// return slow;
// }
// //
----------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------
// Node* kReverse(Node* &head, int k)
// {
//
// // Base Case
// if(head==NULL)
// {
// return NULL;
// }
// int count=0;
// Node* nex=NULL;
// Node* curr=head;
// Node* prev=NULL;
// while(curr!=NULL && count<k)
// {
// nex=curr->next;
// curr->next=prev;
// prev=curr;
// curr=nex;
// count++;
// }
// // prev curr
// // ____curr__ ____nex___ // ____prev__
// // / 12 / - /---->| / 13 / - /-- // / NULL / - /---|
// // --------- | --------- // --------- |
// // | |
// // |----------------------------------------|
// //
// // Step-2 Recursion----------->1st case k ala_wa sb_ko solve kre_ga
// if(nex!=NULL)
// {
// head->next=kReverse(nex,k);
// }
// //
// // return head of reversed list
// print(prev);
// return prev;
// }
// //------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
// //
// void InsertAtBegin(Node* &head, int data) //--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data) //--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void InsertAnyPos(Node* &tail,Node* &head,int position,int data)
// //
// {
// Node* temp=head;
// int cnt=1;
// //
// //
// while(cnt<position-1)
// {
// temp=temp->next;
// cnt++;
// }
// // Inserting at Start
// if(position==1)
// {
// InsertAtBegin(head,data);//------------------------------->head update ho jaa_ye_ga
// return;
// }
// //
// // Inserting at Last
// if(temp->next==NULL)
// {
// InsertAtTail(tail,data);//------------------------------->tail update ho jaa_ye_ga
// return;
// }
// // Insert at any position
// // Creating a new node
// Node *node_to_insert=new Node(data);
// node_to_insert->next=temp->next;
// temp->next=node_to_insert;
// }
// //------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------
// //
// bool isCircular(Node* head)
// {
// if(head==NULL)
// {
// cout<<"List is empty";
// return false;
// }
// Node* temp=head->next;
// while(temp!=NULL&& temp!=head)
// {
// temp=temp->next;
// }
// if(temp==head)
// {
// cout<<"List is circular";
// return true;
// }
// cout<<"List is not circular";
// return false;
// };
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(15);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,14); InsertAtBegin(head,13); InsertAtBegin(head,12); print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,16); InsertAtTail(tail,17); InsertAtTail(tail,18);
// print(node1); cout<<endl;
// cout<<"The value in the Linked List are : "; print(head); cout<<endl;
// // cout<<getLength(head)<<endl;
// // cout<<getMiddleNode(head)->data<<endl;
// // cout<<MiddleNode(head)->data<<endl;
// cout<<isCircular(tail);
// // kReverse(head,2); cout<<endl;
// return 0;
// }
// Detect cycle in a linked list
// Remove cycle from a linked list
// Beginning/ Start of a cycle node in linked list
//
// To detect a cycle 2 methods----> 1) Map 2) Floyd Algorithm {Best approach as S.C.= O(1)}----
> Start of loop nd kr_na pad_ega-------> is_ki help sai removal of loop bhi ho_ga
//
//
// Detect a cycle in a Linked List
// Approach :- make a map in which key is node and bool-------> har element pai ja_ao and map
mai true store kr_te ja_ao
// -----> agar already true stored hai ------> loop is detected
// Time Complexity :- O(n) Space Complexity :- O(n)---> map has n elements
// _________________ _________________ _________________ _________________
// / Data / Address-/---->/ Data / Address-/---->/ Data / Address-/---->/ Data / NODE-2 /---
>-->-|
// ----------------- ----------------- ----------------- ------------------ |
// Node 1 Node-2 Node-3 ............ |
// |_______________________________________________________________|
// bool DetectLoop(Node*head)
// {
// if(head==NULL)
// {
// return false;
// }
// map<Node*, bool> visited;
// Node* temp=head;
// while(temp!=NULL)
// {
// if(visited[temp]==true)
// {
// cout<<"Loop is present at element : "<<temp->data;
// return true;
// }
// visited[temp]=true;
// temp=temp->next;
// }
// return false;
// }
//
//
// Best Approach
// Floyd Cycle Detection Algorithm
// Approach :- Two pointers are created slow and fast ------> Slow-1 Step && Fast->2 Steps at
one time ------->if loop is present then slow and fast point to the same node
// How this approach works ------> When both pointers enters the cycle after
each iteration the distance between slow and fast is getting reduced by 1 step due to which after
some iteration both will point to the same node
// Time Complexity :- O(n) Space Complexity :- O(1)
// bool FloydCycleDetect(Node*head)
// {
// if(head==NULL)
// {
// return false;
// }
// Node* slow=head;
// Node* fast=head;
// Node* temp=head;
// while(temp!=NULL)
// {
fi
// fast=fast->next;
// if(fast->next!=NULL)
// {
// fast=fast->next;
// }
// slow=slow->next;
// if(slow==fast)
// {
// cout<<"Loop is present at ";
// return true;
// }
// }
// return false;
// }
//
//
// ///////////////////////////////////////////////////------------------Starting Node of
Loop-----------------//////////////////////////////////////////////
//
// Approach :- 1) FCD Algo --->Find Point of Intersection
// 2) slow=head & fast= Intersection----> move with same pace 1 step
// 3) jb slow and fast meet kre_nge (slow==fast) ---> start of node hai
//_____________A___________________________ ____________B_______________
// _____ _____ _____ _____ __X__ _____ _____
// / 12 /---->/ 13 /---->/ 14 /---->/ 81 /---->/ 15 /---->/ 16/---->/ 17 /--fast->|
// ---|-- ----- ----- ----- | ----- ----- ----- |
// Slow | |<------Loop C
// | _____ _____ _____ |
// -<--</ 20 /---->/ 19 /---->/ 18 /<----|
// ----- ----- -----
// Hame slow ko head sai point kyu ki_ya and do_no x pai meet kyu kre_nge us_ka reason--->
[15,16,17 represents B] and Agar hame loop complete kr_na hai to ba_ki part ko A ho_na pad_ega
which implies ki loop complete ho gya and since ba_ki part A hai to ham slow ko loop hai u_tha kr
Head pai point kr den_ge
// --->slow and fast do_no same pace sai
move kre_nge jb intersection wa_la node and slow node meet kre_nge to wo starting of node ko
represent kre_nge
// Distance by fast pointer=2 time distance by slow pointer
// Mathematical expression A+B=k times C-------> A+B-->Cycle complete kr_di------>(15,16,17
represent B so Loop bn_ane k lie ba_ki part ko A ho_na pad_ega)
//
// Node* getStartingNode(Node*head)
// {
// if(head==NULL)
// {
// return NULL;
// }
// Node* fast=FloydCycleDetect(head);
// cout<<endl;
// Node* slow=head;
// Node* temp=head;
// while(temp!=NULL)
// {
// fast=fast->next;
// slow=slow->next;
// if(slow==fast)
// {
// cout<<"The head of the cycle is at : "<<slow->data<<endl;
// return slow;
// }
// }
// return NULL;
// }
//
//
//
//
// // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\---Removal of LOOP----\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\//
// //
// // Approach :- start of loop nd kro with getStartingNode and start wale node sai phe_le wale
node k next ko null pai point kr do
// void removeLoop(Node* head)
// {
// if(head==NULL)
// {
// return ;
// }
// Node* startOfLoop=getStartingNode(head);
// Node* temp=startOfLoop;
// while(temp->next!=startOfLoop)
// {
// temp=temp->next;
// }
// temp->next=NULL;
// }
// int main(){
// // Detect a cycle in a Linked List
// return 0;
// }
//
//
//
//
//
//
//
// ///////////////////////////////////////////////////-----------------------------
VERIFICATION---------------------------/////////////////////////////////////////////////
// #include <iostream>
// #include <map>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int dat)
// {
// this->data = dat;
// this->next = NULL;
// }
// ~Node()
// {
// int value = this->data;
// while (this->next != NULL)
// {
// delete next;
// this->next = NULL;
// }
fi
// cout << "Memory is free for node with data : " << value << endl;
// }
// };
// //
// void InsertAtBegin(Node *&head, int data) //--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node *new_node = new Node(data);
// new_node->next = head;
// head = new_node;
// }
// void InsertAtTail(Node *&tail, int data) //--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node *temp = new Node(data);
// tail->next = temp;
// tail = tail->next;
// }
// void print(Node *&head)
// {
// Node *temp = head;
// while (temp != NULL)
// {
// cout << temp->data << " ";
// temp = temp->next;
// }
// cout << endl;
// }
// bool DetectLoop(Node *head)
// {
// if (head == NULL)
// {
// return false;
// }
// map<Node *, bool> visited;
// Node *temp = head;
// while (temp != NULL)
// {
// if (visited[temp] == true)
// {
// cout << "Loop is present at element : " << temp->data;
// return true;
// }
// visited[temp] = true;
// temp = temp->next;
// }
// cout << "Cycle is not present";
// return false;
// }
// Node *FloydCycleDetect(Node *head)
// {
// if (head == NULL)
// {
// return NULL;
// }
// Node *slow = head;
// Node *fast = head;
// while (fast != NULL)
// {
// fast = fast->next;
// if (fast->next != NULL)
// {
// fast = fast->next;
// }
// slow = slow->next;
// if (slow == fast)
// {
// cout << "Cycle is present at :- " << slow->data;
// return slow;
// }
// }
// return NULL;
// }
// //
// // Starting Node of Loop
// // Approach :- 1) FCD Algo --->Find Point of Intersection
// // 2) slow=head & fast= Intersection----> move with same pace 1 step
// // 3) jb slow and fast meet kre_nge (slow==fast) ---> start of node hai
// //_____________A___________________________ ____________B_______________
// // _____ _____ _____ _____ __X__ _____ _____
// // / 12 /---->/ 13 /---->/ 14 /---->/ 81 /---->/ 15 /---->/ 16/---->/ 17 /--fast->|
// // ---|-- ----- ----- ----- | ----- ----- ----- |
// // Slow | |<------Loop C
// // | _____ _____ _____ |
// // -<--</ 20 /---->/ 19 /---->/ 18 /<----|
// // ----- ----- -----
// // Hame slow ko head sai point kyu ki_ya and do_no x pai meet kyu kre_nge us_ka reason--->
[15,16,17 represents B] and Agar hame loop complete kr_na hai to ba_ki part ko A ho_na pad_ega
which implies ki loop complete ho gya and since ba_ki part A hai to ham slow ko loop hai u_tha kr
Head pai point kr den_ge
// // --->slow and fast do_no same pace
sai move kre_nge jb intersection wa_la node and slow node meet kre_nge to wo starting of node
ko represent kre_nge
// // Distance by fast pointer=2 time distance by slow pointer
// // Mathematical expression A+B=k times C-------> A+B-->Cycle complete kr_di------>(15,16,17
represent B so Loop bn_ane k lie ba_ki part ko A ho_na pad_ega)
// //
// Node *getStartingNode(Node *head)
// {
// if (head == NULL)
// {
// return NULL;
// }
// Node *fast = FloydCycleDetect(head);
// cout << endl;
// Node *slow = head;
// Node *temp = head;
// while (temp != NULL)
// {
// fast = fast->next;
// slow = slow->next;
// if (slow == fast)
// {
// cout << "The head of the cycle is at : " << slow->data << endl;
// return slow;
// }
// }
// return NULL;
// }
// // Removal of LOOP
// // Approach :- start of loop nd kro with getStartingNode and start wale node sai phe_le wale
node k next ko null pai point kr do
// void removeLoop(Node *head)
// {
// if (head == NULL)
// {
// return;
// }
// Node *startOfLoop = getStartingNode(head);
// Node *temp = startOfLoop;
// while (temp->next != startOfLoop)
// {
// temp = temp->next;
// }
// temp->next = NULL;
// }
// int main()
// {
// cout << "/////INSERTION AT BEGINNING FOR SINGLE NODE/////" << endl;
// Node *node1 = new Node(19);
// Node *head = node1;
// Node *tail = node1;
// InsertAtBegin(head, 12);
// InsertAtBegin(head, 13);
// InsertAtBegin(head, 14);
// // print(head);
// //
// cout << endl;
// cout << "/////INSERTION AT ENDING FOR SINGLE NODE/////" << endl;
// InsertAtTail(tail, 15);
// InsertAtTail(tail, 16);
// InsertAtTail(tail, 17);
// tail->next = head->next->next;
// // print(node1);
// cout << endl;
// // cout<<"The value in the LINKED LIST are : ";print(head);cout<<endl<<endl;
// // FloydCycleDetect(head);cout<<endl;
// getStartingNode(head);
// cout << endl;
// DetectLoop(head);
// cout << endl;
// removeLoop(head);
// print(head);
// return 0;
// }

// We must read |- 5 ways to remove the loop (1 done) |- To detect cycle 3 ways (2 done)

// Remove duplicate from a sorted and unsorted linked list


//
// /////////////////////////////////////-------------------------1) Remove duplicate from a sorted Linked
List------------------------///////////////////////////////
//
// Approach :- jb_tk curr !=null then curr->data==curr->next->data it means duplicate element is
present & code na fate is_lie check curr->next!=NULL and ----> curr->next--->duplicate element
---> is_se store kr lia and same done for curr->next->next take connection lost na ho
// deleted the node and set curr->next=next_next-------> else curr=curr->next store kr
den_ge
//
// Node* uniqueSortedList(Node* head)
fi
// {
// // empty list
// if(head==NULL)
// {
// return NULL;
// }
// Node *curr=head;
// while(curr!=NULL)
// {
// if((curr->next!=NULL) && curr->data==curr->next->data)
// {
// Node* NodeToDelete=curr->next;
// Node* next_next=curr->next->next;
// delete(NodeToDelete);
// curr->next=next_next;
// }
// else
// {
// // Not empty
// curr=curr->next;
// }
// }
// return head;
// }
//
//
//
// /////////////////////////////////////-------------------------1) Remove duplicate from a unsorted Linked
List------------------------///////////////////////////////
// 3 ways ----> 1) Map < Node, bool > T.C.->O(n) -------------->/12/true/-->/13/true/-->/14/true/--
>/15/true/
// 2) 2 loops-----> T.C.->O(n^2)
// 3) Sort--------> T.C.->O(n_log_n) ---> Sorted wa_la method lga den_ge

// /////////////////////////////////-------------------------1) Veri cation of Remove duplicate from a


unsorted Linked List------------------------///////////////////////////////
//
//
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int dat)
// {
// this->data=dat;
// this->next=NULL;
// }
// ~Node()
// {
// int value=this->data;
// while(this->next!=NULL)
// {
// delete next;
// this->next=NULL;
// }
// cout<<"Memory is free for node with data : "<<value<<endl;
// }
fi
// };
// //
// void InsertAtBegin(Node* &head, int data)//--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data)//--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// // Removing duplicates from a Sorted Linked List
// // Approach :- jb_tk curr !=null then curr->data==curr->next->data it means duplicate element is
present & code na fate is_lie check curr->next!=NULL and ----> curr->next--->duplicate element
---> is_se store kr lia and same done for curr->next->next take connection lost na ho
// // deleted the node and set curr->next=next_next-------> else curr=curr->next store kr
den_ge
// //
// Node* uniqueSortedList(Node* head)
// {
// // empty list
// if(head==NULL)
// {
// return NULL;
// }
// Node *curr=head;
// while(curr!=NULL)
// {
// if((curr->next!=NULL) && curr->data==curr->next->data)
// {
// Node* NodeToDelete=curr->next;
// Node* next_next=curr->next->next;
// delete(NodeToDelete);
// curr->next=next_next;
// }
// else
// {
// // Not empty
// curr=curr->next;
// }
// }
// return head;
// }
// //
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(19);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,12);
// InsertAtBegin(head,13);
// InsertAtBegin(head,14);
// print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,15);
// InsertAtTail(tail,16);
// InsertAtTail(tail,16);
// print(node1);
// cout<<"\nThe value in the linked list are : ";
// print(head);
// cout<<endl;
// uniqueSortedList(head);
// cout<<"The value in the linked list after removing duplicates are : ";
// print(head);
// //
// return 0;
// }

//

// SORT 0's, 1's & 2's IN A LINKED LIST


// ------------------------------------------------------------: Approach
1 :---------------------------------------------------------
// // Approach 1 :- 3 variable mai 0 1 and 2 ka count store kr lo and then series wise us_se print
kr do
// // Time complexity->O(n) Space complexity:-O(1)
// Node *sortList(Node *head)
// {
// int count0=0;
// int count1=0;
// int count2=0;
// Node* temp=head;
// while(temp!=NULL)
// {
// if(temp->data==0)
// {
// count0++;
// }
// if(temp->data==2)
// {
// count2++;
// }
// if(temp->data==1)
// {
// count1++;
// }
// temp=head;
// while(temp!=NULL)
// {
// if(temp->data==0)
// {
// cout<<temp->data<<" ";
// count0--;
// }
// if(temp->data==1)
// {
// cout<<temp->data<<" ";
// count1--;
// }
// if(temp->data==2)
// {
// cout<<temp->data<<" ";
// count2--;
// }
// }
// }
// return head;
// }
//
//
// ------------------------------------------------------------: Approach
2 :---------------------------------------------------------
//
// Approach 2 :- Data replacement is not allowed then we change links
// Create 3 list to store 0's,1's and 2's and merge them and ensure the head of all three
list start with a dummy node---> why ? ---> to minimize the number of if conditions while merging
// // Time complexity->O(n) Space complexity:-O(1)
// Node *InsertAtTail(Node *tail, Node *curr)
// {
// tail->next=curr;
// tail=curr;
// }
// Node *SortList(Node *head)
// {
// Node *zeroHead=new Node(-1);//--------------|
// Node *zeroTail=zeroHead;// |
// Node *oneHead=new Node(-1);//---------------|---------> Dummy Node
// Node *oneTail=oneHead;// |
// Node *twoHead=new Node(-1);//---------------|
// Node *twoTail=twoHead;
// //
// Node *curr=head;
// // Create separate list 0's , 1's and 2's
// // / zero_dummy /--->/0/---->/0/ / one_dummy /--->/1/---->/1/ / two_dummy /--->/
2/---->/2/
// while(curr!=NULL)
// {
// int value=curr->data;
// if(value==0)
// {
// InsertAtTail(zeroTail,curr);
// }
// if(value==1)
// {
// InsertAtTail(oneTail,curr);
// }
// if(value==2)
// {
// InsertAtTail(twoTail,curr);
// }
// curr=curr->next;
// }
// // Merge 3 sublist
// // one/twoHead ka next is_lie li_ya kyu_ki dummy node ko include nhi kr_na tha
// // / zero_dummy /--->/0/---->/0/---|
// // |-------<-----|
// // |
// // / one_dummy /--->/1/---->/1/-----|
// // |
// // |-------<------|
// // / two_dummy /--->/2/---->/2/
// if(oneHead->next!=NULL)
// {
// //Non empty 1's list
// zeroTail->next=oneHead->next;
// }
// else
// {
// //Empty 1's list
// zeroTail->next=twoHead->next;
// }
// oneTail->next=twoTail->next;
// twoTail->next=NULL;
// // |---/one_dummy/
// // Here our linked list looked like this / zero_dummy /--->/0/---->/0/---->/1/---->/1/---->/2/---->/
2/
// // |---/two_dummy/
// head=zeroHead->next;
// //delete dummy nodes
// delete zeroHead;
// delete oneHead;
// delete twoHead;
// }

// ////////////////////////////////////--------------------------------Veri cation of sort 0, 1 and


2---------------------------------------------///////////////////////////////////
// #include<iostream>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int dat)
// {
// this->data=dat;
// this->next=NULL;
// }
// // ~Node()
// // {
// // int value=this->data;
// // while(this->next!=NULL)
// // {
// // delete next;
// // this->next=NULL;
// // }
// // cout<<"Memory is free for node with data : "<<value<<endl;
// // }
// };
// //
// void InsertAtBegin(Node* &head, int data)//--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
fi
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data)//--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// // ------------------------------------------------------------: Approach
1 :---------------------------------------------------------
// Node *sortList(Node *head)
// {
// int count0=0;
// int count1=0;
// int count2=0;
// Node* temp=head;
// while(temp!=NULL)
// {
// if(temp->data==0)
// {
// count0++;
// }
// if(temp->data==1)
// {
// count1++;
// }
// if(temp->data==2)
// {
// count2++;
// }
// temp=temp->next;
// }
// temp=head;
// while(temp!=NULL)
// {
// if(count0!=0)
// {
// temp->data=0;
// count0--;
// }
// else if(count1!=0)
// {
// temp->data=1;
// count1--;
// }
// else if(count2!=0)
// {
// temp->data=2;
// count2--;
// }
// temp=temp->next;
// }
// return head;
// }
// //
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// // ------------------------------------------------------------: Approach
2 :---------------------------------------------------------
// Node *InsertAtTail(Node *&tail, Node *curr)
// {
// tail->next=curr;
// tail=curr;
// }
// Node *SortList(Node *&head)
// {
// Node *zeroHead=new Node(-1);//--------------|
// Node *zeroTail=zeroHead;// |
// Node *oneHead=new Node(-1);//---------------|---------> Dummy Node
// Node *oneTail=oneHead;// |
// Node *twoHead=new Node(-1);//---------------|
// Node *twoTail=twoHead;
//
// Node *curr=head;
// // Create separate list 0's , 1's and 2's
// // / zero_dummy /--->/0/---->/0/ / one_dummy /--->/1/---->/1/ / two_dummy /--->/
2/---->/2/
// while(curr!=NULL)
// {
// int value=curr->data;
// if(value==0)
// {
// InsertAtTail(zeroTail,curr);
// }
// if(value==1)
// {
// InsertAtTail(oneTail,curr);
// }
// if(value==2)
// {
// InsertAtTail(twoTail,curr);
// }
// curr=curr->next;
// }
// // Merge 3 sublist
// // one/twoHead ka next is_lie li_ya kyu_ki dummy node ko include nhi kr_na tha
// // / zero_dummy /--->/0/---->/0/---|
// // |-------<-----|
// // |
// // / one_dummy /--->/1/---->/1/-----|
// // |
// // |-------<------|
// // / two_dummy /--->/2/---->/2/
// if(oneHead->next!=NULL)
// {
// //Non empty 1's list
// zeroTail->next=oneHead->next;
// }
// else
// {
// //Empty 1's list
// zeroTail->next=twoHead->next;
// }
// oneTail->next=twoHead->next;
// twoTail->next=NULL;
// // |---/one_dummy/
// // Here our linked list looked like this / zero_dummy /--->/0/---->/0/---->/1/---->/1/---->/2/---->/
2/
// // |---/two_dummy/
// head=zeroHead->next;
// //delete dummy nodes-----------------------> Comment out destructor to run this part as it will
delete all the nodes
// delete zeroHead;
// delete oneHead;
// delete twoHead;
// return head;
//
// }
// //
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
// Node* node1=new Node(1);
// Node* head=node1;
// Node* tail=node1;
// InsertAtBegin(head,0);
// InsertAtBegin(head,1);
// InsertAtBegin(head,0);
// print(head);
// //
// cout<<endl;
// cout<<"/////INSERTION AT ENDING FOR SINGLE NODE/////"<<endl;
// InsertAtTail(tail,2);
// InsertAtTail(tail,1);
// InsertAtTail(tail,2);
// print(node1);
// cout<<endl;
// cout<<"The value in the linked list are : ";print(head);
// cout<<endl;
// SortList(head);
// cout<<"The value in the linked list are : ";print(head);
// //
// return 0;
// }

//

// // Merge Two Sorted Linked List Optimized approach


// // Approach :- Check if element of other list can be put up between elements of 2nd list
// // MERGED LIST
// // H1--->/1/---|->/3/--->/5/--->NULL-------------|
// // prev<---| | |----->curr |----------> H1---->/1/--->/2/--->/3/--->/4/--->/5/---
>/5/--->NULL
// // H2------->/2/--->/4/--->/5/--->NULL-----------|
// // |----->True then pointers k beech mai da_al do & curr
and prev pointer ko update kr do
// // Condition :- prev->data <= temp->data <= curr->data <-------|
// // |----->False then curr pointer ko aa_ge bad_ha do
// #include<iostream>
// #include<vector>
// using namespace std;
// class Node
// {
// public:
// int data;
// Node *next;
// Node(int dat)
// {
// this->data=dat;
// this->next=NULL;
// }
// ~Node()
// {
// int value=this->data;
// while(this->next!=NULL)
// {
// delete next;
// this->next=NULL;
// }
// cout<<"Memory is free for node with data : "<<value<<endl;
// }
// };
// //
// void InsertAtBegin(Node* &head, int data)//--------------->Create a Head Pointer and-----
>Starting point ko denote kre------->Make new node as head node
// {
// Node* new_node=new Node(data);
// new_node->next=head;
// head=new_node;
// }
// void InsertAtTail(Node* &tail, int data)//--------------->Create a Tail Pointer and----->Last node
ko denote kre----->Ending Node k aa_ge new node ko add kre_nge
// {
// Node* temp=new Node(data);
// tail->next=temp;
// tail=tail->next;
// }
// void print(Node* &head)
// {
// Node *temp=head;
// while(temp!=NULL)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<endl;
// }
// //
// // MERGING TWO LINKED LIST
// Node* solve(Node* & rst, Node* &second)
// {
// // if only one element is present in rst list
// if ( rst->next==NULL)
// {
// rst->next=second;
// return rst;
// }
// // curr1 next1
// // H1--->/1/--|->/3/--->/5/--->NULL
// // H2------->/2/--->/4/--->/5/--->NULL
fi
fi
fi
fi
fi
// // curr2 next2
// Node *curr1= rst;
// Node *next1=curr1->next;
// Node *curr2=second;
// Node *next2=curr2->next;
// //
// while(next1!=NULL && curr2!=NULL)
// {
// if(curr2->data>=curr1->data && curr2->data<=next1->data)
// {
// //add nodes in between the rst list
// // curr2->next=curr1->next;
// curr1->next=curr2;
// next2=curr2->next;
// curr2->next=next1;
// //update pointers
// curr1=curr2;
// curr2=next2;
// }
// else
// {
// // curr1 & next1 ko aa_ge bad_ha dena
// curr1=next1;
// next1=next1->next;
// if(next1==NULL)
// {
// curr1->next=curr2;
// return rst;
// }
// }
// }
// print( rst);
// return rst;
// //
// }
// Node *MergeTwoList(Node *& rst, Node *&second)
// {
// if( rst==NULL)
// {
// return second;
// }
// if(second==NULL)
// {
// return rst;
// }
// if( rst->data<=second->data)
// {
// Node* temp=solve( rst,second);
// print(temp);
// return solve( rst,second);
// }
// else
// {
// Node* temp=solve(second, rst);
// print(temp);
// return solve(second, rst);
// }
// }
// int main(){
// cout<<"/////INSERTION AT BEGINNING FOR SINGLE NODE/////"<<endl;
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
// Node* node1=new Node(15);
// Node* node2=new Node(18);
// Node* head1=node1;
// Node* head2=node2;
// Node* tail1=node1;
// InsertAtBegin(head1,14);
// InsertAtBegin(head1,13);
// InsertAtBegin(head1,12);
// print(head1);
// InsertAtBegin(head2,17);
// InsertAtBegin(head2,16);
// InsertAtBegin(head2,15);
// print(head2);
// MergeTwoList(head2,head1);
// return 0;
// }

You might also like