From d7c1a4cd7b74b350e33dbaa4fff6e940a333e9c8 Mon Sep 17 00:00:00 2001 From: Vamika Arya Date: Tue, 14 Jan 2025 15:20:27 +0530 Subject: [PATCH 1/5] added file for Split_Circular_List_into_2_halves --- .../Split_Circular_List_into_2_halves.cpp | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp 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"< Date: Tue, 14 Jan 2025 19:06:33 +0530 Subject: [PATCH 2/5] added file/approach-1/sort_LL of 0s,1s,2s --- .../sort_approach-1.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Lecture049 Linked List Day6/sort_approach-1.cpp diff --git a/Lecture049 Linked List Day6/sort_approach-1.cpp b/Lecture049 Linked List Day6/sort_approach-1.cpp new file mode 100644 index 00000000..8c95c6f7 --- /dev/null +++ b/Lecture049 Linked List Day6/sort_approach-1.cpp @@ -0,0 +1,66 @@ +#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!=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"< Date: Tue, 14 Jan 2025 23:03:17 +0530 Subject: [PATCH 3/5] added file for merging linked lists --- Lecture049 Linked List Day6/merge_LL-2.cpp | 165 +++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 Lecture049 Linked List Day6/merge_LL-2.cpp diff --git a/Lecture049 Linked List Day6/merge_LL-2.cpp b/Lecture049 Linked List Day6/merge_LL-2.cpp new file mode 100644 index 00000000..3118e59e --- /dev/null +++ b/Lecture049 Linked List Day6/merge_LL-2.cpp @@ -0,0 +1,165 @@ +#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!=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"< Date: Wed, 15 Jan 2025 15:27:11 +0530 Subject: [PATCH 4/5] added file/palindrome_alter_approach --- Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp diff --git a/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp b/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp new file mode 100644 index 00000000..e69de29b From d5841c69cdf3a93e1cca19860a4d5774cac36d9f Mon Sep 17 00:00:00 2001 From: Vamika Arya Date: Fri, 17 Jan 2025 19:38:06 +0530 Subject: [PATCH 5/5] added code/clone LL-approach 1 --- .../palindrome_LL-approach-2.cpp | 38 +++++++++++++++++++ .../clone_LL_random-ptr-approach-1.cpp | 0 2 files changed, 38 insertions(+) create mode 100644 Lecture052 Linked List Day9/clone_LL_random-ptr-approach-1.cpp diff --git a/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp b/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp index e69de29b..12f7794e 100644 --- a/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp +++ b/Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp @@ -0,0 +1,38 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + bool isPalindrome(ListNode* head) { + ListNode* prev=NULL; + ListNode* curr=new ListNode(head->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