Professional Documents
Culture Documents
Linked List
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;
// }
// We must read |- 5 ways to remove the loop (1 done) |- To detect cycle 3 ways (2 done)
//
//