diff --git a/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp b/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp new file mode 100644 index 00000000..a4dd0a96 --- /dev/null +++ b/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +class Node{ + public: + int data; + Node* next; + Node(int data){ + this->data=data; + this->next=NULL; + } +}; +void print_LL (Node* &head){ + Node* temp=head; + while(temp->next!=head){ + cout<data<<" "; + temp=temp->next; + } + cout<data<next=head; + tail->next=new_node; + head=new_node; +} +int length(Node* &head){ + int length=1; + Node* temp=head; + while(temp->next!=head){ + length++; + temp=temp->next; + } + return length; +} +void splitinto2(Node* &head,Node* &tail){ + Node* temp=head; + int pivot=0; +//finding node pos about which to split CLL + int size=length(head); + if(size%2==0){pivot=size/2;} + else{pivot=(size+1)/2;} + int count=1; + while(count!=pivot){ + temp=temp->next; + count++; + } + Node* head2=temp->next; + temp->next=head; + cout<<"First Half of given CLL"<next=head2; + cout<<"Second Half of given CLL"< +using namespace std; +class Node{ + public: + int data; + Node* next; + Node(int data){ + this->data=data; + this->next=NULL; + } +}; + + +void print_LL (Node* &head){ + Node* temp=head; + while(temp->next!=NULL){ + cout<data<<" "; + temp=temp->next; + } + cout<data<next=head; + + head=new_node; +} + + +void sort(Node* &head){ + int count_0=0; + int count_1=0; + int count_2=0; + Node* temp=head; + while(temp!=NULL){ + if(temp->data==0){count_0++;} + else if(temp->data==1){count_1++;} + else{count_2++;} + temp=temp->next; + } + temp=head; + for(int i=1;i<=count_0;i++){ + temp->data=0; + temp=temp->next; + + } + for(int i=1;i<=count_1;i++){ + temp->data=1; + temp=temp->next; + } + for(int i=1;i<=count_2;i++){ + temp->data=2; + temp=temp->next; + } +} + + +void InsertAtTail(Node* &tail,Node* &node_copy){ + tail->next=node_copy; + tail=node_copy; +} + + +void sort2(Node* &head){ + Node* copy=NULL; + Node* temp=head; +//zeros LL first node + Node* zerohead=new Node(-1);//dummy node + Node* zerotail=zerohead; +//ones LL first node + Node* onehead=new Node(-1);//dummy node + Node* onetail=onehead; +//zeros LL twos node + Node* twohead=new Node(-1);//dummy node + Node* twotail=twohead; +//3 separate LL for storing 0s,1s,2s + while(temp!=NULL){ + if(temp->data==0){ + Node* copy=new Node(0); + InsertAtTail(zerotail,copy);} + else if(temp->data==1){ + Node* copy=new Node(1); + InsertAtTail(onetail,copy);} + else{ + Node* copy=new Node(2); + InsertAtTail(twotail,copy);} + temp=temp->next; + } +//merging above 3 LL + zerotail->next=onehead->next; + if(onehead->next==NULL){zerotail->next=twohead->next;} + else{onetail->next=twohead->next;} + Node* head_0=zerohead->next; + Node* head_1=onehead->next; + Node* head_2=twohead->next; + delete onehead; + delete twohead; + delete zerohead; + head=head_0; +} + +//merging two linked lists +void merge(Node* &head1,Node* &head2){ + Node* temp1=head1; + Node* temp2=head2; + while(temp2!=NULL){ + temp1=head1; + bool inserted=false; + Node* copy=new Node(temp2->data); + while(temp1->next!=NULL){ + if(temp2->data<=head1->data){ + copy->next=temp1; + head1=copy; + temp1=head1; + inserted=true; + break; + } + else if(temp2->data>=temp1->data && temp2->data<=temp1->next->data){ + copy->next=temp1->next; + temp1->next=copy; + temp1=temp1->next; + inserted=true; + break; + } + else{temp1=temp1->next;} + } + if(inserted=false){temp1->next=copy;} + temp2=temp2->next; + } +} + +int main(){ + Node* node1=new Node(0); + Node* head=node1; + Node* tail=node1; + Node* node2=new Node(2); + Node* head2=node2; + Node* tail2=node2; + InsertAtHead(1,head); + InsertAtHead(0,head); + InsertAtHead(0,head); + InsertAtHead(2,head); + InsertAtHead(2,head2); + InsertAtHead(1,head2); + InsertAtHead(0,head2); + InsertAtHead(2,head2); + InsertAtHead(2,head2); + InsertAtHead(1,head2); + cout<<"LINKED LIST 1 BEFORE SORTING"< +using namespace std; +class Node{ + public: + int data; + Node* next; + Node(int data){ + this->data=data; + this->next=NULL; + } +}; +void print_LL (Node* &head){ + Node* temp=head; + while(temp->next!=NULL){ + cout<data<<" "; + temp=temp->next; + } + cout<data<next=head; + head=new_node; +} +void sort(Node* &head){ + int count_0=0; + int count_1=0; + int count_2=0; + Node* temp=head; + while(temp!=NULL){ + if(temp->data==0){count_0++;} + else if(temp->data==1){count_1++;} + else{count_2++;} + temp=temp->next; + } + temp=head; + for(int i=1;i<=count_0;i++){ + temp->data=0; + temp=temp->next; + + } + for(int i=1;i<=count_1;i++){ + temp->data=1; + temp=temp->next; + } + for(int i=1;i<=count_2;i++){ + temp->data=2; + temp=temp->next; + } +} +int main(){ + Node* node1=new Node(0); + Node* head=node1; + Node* tail=node1; + InsertAtHead(1,head); + InsertAtHead(0,head); + InsertAtHead(0,head); + InsertAtHead(2,head); + InsertAtHead(0,head); + cout<<"LINKED LIST BEFORE SORTING"<val); + ListNode* temp=head; + ListNode* forward=NULL; + while(temp!=NULL){ + if(temp->next!=NULL){forward=new ListNode(temp->next->val);} + curr->next=prev; + prev=curr; + curr=forward; + temp=temp->next; + } + temp=head; + while(prev!=NULL){ + if(prev->val==temp->val){ + prev=prev->next; + temp=temp->next; + } + else{ + return false; + } + } + return true; + + } +}; \ No newline at end of file diff --git a/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-1.cpp b/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-1.cpp new file mode 100644 index 00000000..e69de29b diff --git a/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-2.cpp b/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-2.cpp new file mode 100644 index 00000000..9c0a45b2 --- /dev/null +++ b/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-2.cpp @@ -0,0 +1,50 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; +*/ +void InsertAtTail(Node* &node, Node* &tail){ + tail->next=node; + tail=node; +} + +class Solution { +public: + Node* copyRandomList(Node* head) { + unordered_map map; + Node* temp=head; + Node* copy_head=NULL; + Node* copy_tail=NULL; +//copying LL through next pointer + while(temp!=NULL){ + Node* copy=new Node(temp->val); //deep copy of current temp node + map[temp]=copy; //mapping original and its copy node + if(copy_head==NULL){ + copy_head=copy; + copy_tail=copy; + } + else{InsertAtTail(copy,copy_tail);} + temp=temp->next; + } +//cloning random pointer connections through map + temp=head; + Node* temp2=copy_head; + while(temp!=NULL && temp2!=NULL){ + Node* curr_random=temp->random; + temp2->random=map[curr_random]; + temp=temp->next; + temp2=temp2->next; + } + return copy_head; + } +}; \ No newline at end of file diff --git a/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-3 b/Lecture052 Linked List Day9/clone_LL_random-ptr-approach-3 new file mode 100644 index 00000000..e69de29b