You are on page 1of 6

Python & Libraries Quickstarter

This is a quickstart section, please read this document before proceeding with the
videos in the next part.

1) This section is designed for advanced programmers who are already familiar with
Python and Programming Concepts and topics like Data Structures in Python. These
students can skip Python Basics videos and can jump to Machine Learning section and
get their hands dirty with code. 😉

2) The quickstart sections are about Python Libraries, which give you a head-start
if you want to start Machine Learning in less time. For more detailed introduction
to libraries watch the respective sections of Numpy, Pandas, Matplotlib etc. You
can install these libraries using pip. For example -

pip install numpy


pip install pandas
pip install matplotlib

3) If you are new to programming, then the sections starting from Python Basics are
recommended. You won't miss anything if you start from Python Basics (next Module)
and skip this one.

P1
D

P2
A

P3
C

P4
A

P5
B

P6
D

P7
Node* insertInMiddle(Node* head, int x)
{
Node *temp=new Node(x);
Node *slow,*fast;
if(head==NULL){
head=temp;
return head;
}
slow=head;
fast=head->next;
while(fast&& fast->next){
slow=slow->next;
fast=fast->next->next;
}
temp->next=slow->next;
slow->next=temp;
return head;
}

P8
void deleteNode(Node *node)
{

node->data = node->next->data;
node->next = node->next->next;

P9 a)

class Solution {
public:
ListNode* sortList(ListNode* head) {
struct ListNode *ptr,*cpt;
int temp;
ptr=head;
if(head==NULL)
return head;
while(ptr->next!=NULL){
cpt=ptr->next;
while(cpt!=NULL){
if(ptr->val>cpt->val){
temp=ptr->val;
ptr->val=cpt->val;
cpt->val=temp;
}
cpt=cpt->next;

}
ptr=ptr->next;
}
return head;
}

};

P9 b)
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
map<int,ListNode*>mp;
ListNode* curr=head;
int sum=0;
while(curr){
sum=sum+curr->val;
if(sum==0){
head=curr->next;
mp.clear();
curr=curr->next;
continue;

}
if(mp.find(sum)!=mp.end()){
ListNode* alpha=mp[sum];
ListNode* left=alpha->next;
int deleteSum=sum;
while(left!=curr){
deleteSum=left->val;
mp.erase(deleteSum);
left=left->next;
}
alpha->next=curr->next;
curr=curr->next;
}
else{
mp[sum]=curr;
curr=curr->next;
}

}
return head;
}
};

P10
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* t = new ListNode(0);
t->next = head;

ListNode *p = t;
while(p->next!=NULL&&p->next->next!=NULL){
if(p->next->val == p->next->next->val){
int dup = p->next->val;
while(p->next!=NULL&&p->next->val==dup){
p->next = p->next->next;
}
}else{
p=p->next;
}

}
return t->next;

}
};

P11

int intersectPoint(Node* head1, Node* head2)


{
int c1=0,c2=0;
Node*p=head1;
Node*q=head2;

while(p->next!=NULL)
{
c1++;
p=p->next;
}
while(q->next!=NULL)
{
c2++;
q=q->next;
}
int d=abs(c1-c2);
p=head1;
q=head2;
if(c1>c2)
{
for(int i=0; i<d; i++)
p=p->next;

}
else
{

for(int i=0; i<d; i++)


q=q->next;
}
while(p!=NULL && q!=NULL)
{
if(p==q)
{
return p->data;
}
p=p->next;
q=q->next;
}
return -1;

P12
Node* segregate(Node *head) {

// Add code here


Node *ptr=head;
int cnt[3]={0,0,0};
while(ptr!=NULL){
cnt[ptr->data]++;
ptr=ptr->next;
}
int i=0;
ptr=head;
while(ptr!=NULL){
if(cnt[i]==0)
i++;
else{
ptr->data=i;
cnt[i]--;
ptr=ptr->next;
}
}
return head;

P13

bool detectLoop(Node* head)


{
Node* slow=head;
Node *fast =head;
if(head==NULL){
return head;
}
while(fast && fast->next){

slow=slow->next;
fast=fast->next->next;
if(slow==fast)
return true;
}
return false;
}

P14

int countNodesinLoop(struct Node *head)


{
Node *slow = head, *fast = head;

while (slow && fast &&


fast->next)
{
slow = slow->next;
fast = fast->next->next;

if (slow== fast){
int res = 1;
Node *temp = slow;
while (temp->next != slow)
{
res++;
temp = temp->next;
}
return res;
}
}

/* Return 0 to indeciate that


their is no loop*/
return 0;
}

You might also like