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

Home Work Question related to Lect-48 added #542

Open
wants to merge 2 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
85 changes: 85 additions & 0 deletions Lecture048 Linked List Day5/Remove Duplicates From an Unsorted LL
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
/****************************************************************
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<int, bool> 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<int, bool> 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;
}

90 changes: 90 additions & 0 deletions Lecture048 Linked List Day5/Split A Circular Linked List.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Split A Circular Linked List: (Coding Ninja)
https://www.naukri.com/code360/problems/split-a-circular-linked-list_1071003

#include <bits/stdc++.h>
/********************************
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);
}