From 0e85340f252a7611433587e96a75b9a3832ff844 Mon Sep 17 00:00:00 2001 From: Vivek Nikate Date: Tue, 23 Apr 2024 17:51:26 +0530 Subject: [PATCH 1/2] Create Remove Duplicates From an Unsorted LL Added HomeWork Question Remove Duplicates From an Unsorted Linked List --- .../Remove Duplicates From an Unsorted LL | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Lecture048 Linked List Day5/Remove Duplicates From an Unsorted LL diff --git a/Lecture048 Linked List Day5/Remove Duplicates From an Unsorted LL b/Lecture048 Linked List Day5/Remove Duplicates From an Unsorted LL new file mode 100644 index 00000000..a25cc8f3 --- /dev/null +++ b/Lecture048 Linked List Day5/Remove Duplicates From an Unsorted LL @@ -0,0 +1,85 @@ +Q.Remove Duplicates From an Unsorted Linked List: + https://www.naukri.com/code360/problems/remove-duplicates-from-unsorted-linked-list_1069331 + Solution:- + + // Not Optimized: using map + #include + /**************************************************************** + Following is the class structure of the Node class: + class Node { + public: + int data; + Node *next; + Node(int data) { + this->data = data; + this->next = NULL; + } + }; + *****************************************************************/ + Node *removeDuplicates(Node *head) { + Node *cur = head; + Node *prev = NULL; + map vis; + + while(cur){ + if(vis[cur -> data]){ + Node *temp = cur; + + prev -> next = cur -> next; + cur = cur -> next; + + delete temp; + }else{ + vis[cur -> data] = 1; + prev = cur; + cur =cur -> next; + } + } + return head; + } + + // or TC: O(N²) & SC: O(1), Using loop + Node *removeDuplicates(Node *head){ + Node *i, *j; + i = head; + while(i){ + j = i -> next; + Node *prev = i; + + while(j){ + if(i -> data == j -> data){ + prev -> next = j -> next; + + delete j; + + j = prev -> next; + }else{ + prev = j; + j= j-> next; + } + } + i = i-> next; + } + return head; + } + + // Optimized: using Map + Node *removeDuplicates(Node *head){ + unordered_map vis; + if(!head) + return head; + + vis[head -> data] = 1; + Node *cur = head; + + while(cur -> next){ + if(vis[cur -> next -> data]){ + cur -> next = cur -> next -> next; + }else{ + vis[cur -> next -> data] = 1; + cur = cur -> next; + } + } + return head; + } + From 23f2cff824993883a862717a5722307e0e5e1d38 Mon Sep 17 00:00:00 2001 From: Vivek Nikate Date: Tue, 23 Apr 2024 17:58:28 +0530 Subject: [PATCH 2/2] HomeWork Split CircularLL solution file added --- .../Split A Circular Linked List.txt | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Lecture048 Linked List Day5/Split A Circular Linked List.txt diff --git a/Lecture048 Linked List Day5/Split A Circular Linked List.txt b/Lecture048 Linked List Day5/Split A Circular Linked List.txt new file mode 100644 index 00000000..a5a00530 --- /dev/null +++ b/Lecture048 Linked List Day5/Split A Circular Linked List.txt @@ -0,0 +1,90 @@ +Split A Circular Linked List: (Coding Ninja) + https://www.naukri.com/code360/problems/split-a-circular-linked-list_1071003 + + #include + /******************************** + class Node { + public: + int data; + Node *next; + Node(int data) { + this->data = data; + this->next = NULL; + } + }; + ********************************/ + // Solution 1: + void splitCircularList(Node *head) { + if(head == NULL) + return ; + + Node *slow, *fast; + slow = fast = head; + + while(fast -> next != head){ + fast = fast -> next; + if(fast -> next == head) + break; + + fast = fast -> next; + slow = slow -> next; + } + fast -> next = slow -> next; + slow -> next = head; + } + + // Solution 2:Brute Force: + int getLen(Node *h){ + int l=0; + Node *Same = h; + + while(h){ + l++; + h = h-> next; + + if(Same == h) + break; + } + return l; + } + void splitCircularList(Node *head) { + if(!head) + return ; + int len = getLen(head) / 2; + Node *first = head; + + while(--len) + first = first -> next; + + Node *second = first -> next; + Node *temp = second; + + while(second -> next != head) + second = second -> next; + + first -> next = head; + second -> next = temp; + } + +Split a Circular Linked List into two halves: (GFG) + https://www.geeksforgeeks.org/problems/split-a-circular-linked-list-into-two-halves/1 + // Solution: + void splitList(Node *head, Node **head1_ref, Node **head2_ref) { + Node *slow = head; + Node *fast = head; + + while(fast -> next != head){ + fast = fast -> next; + if(fast -> next == head) + break; + + fast = fast -> next; + slow = slow -> next; + } + + fast -> next = slow -> next; + slow -> next = head; + + *head1_ref = head; + *head2_ref = (fast -> next); + }