Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning Linked List with random pointer : Approach 3 #578

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
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<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void InsertAtHead(int data,Node* &head,Node* tail){
Node* new_node=new Node(data);
new_node->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"<<endl;
print_LL(head);
tail->next=head2;
cout<<"Second Half of given CLL"<<endl;
print_LL(head2);
}
int main(){
Node* node1=new Node(100);
Node* head=node1;
Node* tail=node1;
InsertAtHead(10,head,tail);
InsertAtHead(20,head,tail);
InsertAtHead(30,head,tail);
InsertAtHead(40,head,tail);
InsertAtHead(50,head,tail);
cout<<"COMPLETE CIRCULAR LINKED LIST"<<endl;
print_LL(head);
splitinto2(head,tail);
return 0;
}
165 changes: 165 additions & 0 deletions Lecture049 Linked List Day6/merge_LL-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <iostream>
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<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}


void InsertAtHead(int data,Node* &head){
Node* new_node=new Node(data);
new_node->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"<<endl;
print_LL(head);
cout<<"LINKED LIST 1 AFTER SORTING"<<endl;
sort2(head);
print_LL(head);
cout<<"\nLINKED LIST 2 BEFORE SORTING"<<endl;
print_LL(head2);
cout<<"LINKED LIST 2 AFTER SORTING"<<endl;
sort2(head2);
print_LL(head2);
cout<<"LINKED LIST AFTER MERGING ABOVE LINKED LISTS"<<endl;
merge(head,head2);
print_LL(head);
return 0;
}
66 changes: 66 additions & 0 deletions Lecture049 Linked List Day6/sort_approach-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
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<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void InsertAtHead(int data,Node* &head){
Node* new_node=new Node(data);
new_node->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"<<endl;
print_LL(head);
cout<<"LINKED LIST AFTER SORTING"<<endl;
sort(head);
print_LL(head);
return 0;
}
38 changes: 38 additions & 0 deletions Lecture050 Linked List Day7/palindrome_LL-approach-2.cpp
Original file line number Diff line number Diff line change
@@ -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;

}
};
Empty file.
50 changes: 50 additions & 0 deletions Lecture052 Linked List Day9/clone_LL_random-ptr-approach-2.cpp
Original file line number Diff line number Diff line change
@@ -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<Node*,Node*> 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;
}
};
Empty file.