0% found this document useful (0 votes)
23 views

DSAa

The code takes in the head of a linked list and reverses it recursively by swapping the next pointers and returning the new head; it starts by base cases of an empty list or list with one node, otherwise it recursively calls the reverse function on the rest of the list and updates the pointers to reverse the first node.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

DSAa

The code takes in the head of a linked list and reverses it recursively by swapping the next pointers and returning the new head; it starts by base cases of an empty list or list with one node, otherwise it recursively calls the reverse function on the rest of the list and updates the pointers to reverse the first node.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

struct node *reverseIt (struct node *head, int k)

{
// Complete this method
struct node *first=head;
struct node *first2=head;
struct node *first3=head;
struct node *temp = NULL;
struct node *middle=NULL;
struct node *trail;
int c=0;
int s=0;
int arr[50];
while(first2)
{
arr[s]=first2->data;
first2=first2->next;
s++;
}
int a=s%k;
int b=s/k;
if(k>0){
while(c<k && first)
{
trail=middle;
middle=first;
first=first->next;
middle->next=trail;
c++;
}c=0;
while(s>c)
{
int k=s;
temp=new node(arr[k-1]);
first3->next=temp;
first3=temp;
s--;
}
head=middle;
}
return head;

}
User
Manav
5 months ago

C++ solution using recursion

class Solution
{
public:
struct node *reverse (struct node *head, int k)
{
if(head == NULL || head->next == NULL){
return head;
}

struct node*next = NULL;


struct node*curr = head;
struct node*prev= NULL;
int cnt = 0;
while(curr!=NULL and cnt<k)
{
next = curr ->next;
curr->next = prev;
prev = curr;
curr = next;
cnt++;
}
if(next!=NULL)
{
head->next = reverse(next,k);
}
return prev;
}
};
Intersection of two arrays
int intersectPoint(Node* head1, Node* head2)
{
// Your Code Here
Node *p=head1;
Node *p1=head1;

while(p1)
{
Node *p2=head2;
while(p2){
if(p1->data==p2->data)
{
Node *p3=p1;
Node *p4=p2;
int i=p1->data;
while(p3 && p4)
{
if(p3->data==p4->data)
{
p3=p3->next;
p4=p4->next;
}
}
if(p3==NULL&&p4==NULL)
return i;
}p2=p2->next;
}
p1=p1->next;

}
Merge sort in linked list
class Solution{
public:
//Function to sort the given linked list using Merge Sort.
Node* middle(Node *head)
{
Node *slow=head;
Node *fast=head->next;
while(fast!=NULL && fast->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
/*Node* insertend(Node *head,int data)
{
Node *p=head;

if(head==NULL)
{
head=new Node(data);
}
else
{
Node *temp=new Node(data);
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
return head;
}*/
Node* merge(Node *left,Node *right)
{
Node *head=new Node(-1);
Node *tail=head;
Node *p=left;
Node *q=right;
if(left==NULL)
{
return right;
}
if(right==NULL)
{
return left;
}
while(p && q)
{
if(p->data<q->data)
{
tail->next=p;
tail=p;
p=p->next;
}
else
{
tail->next=q;
tail=q;
q=q->next;
}
}

while(q!=NULL){
tail->next=q;
tail=q;
q=q->next;

}
while(p!=NULL)
{
tail->next=p;
tail=p;
p=p->next;
}

head=head->next;
return head;
}

Node* mergeSort(Node* head) {


// your code here
if(head==NULL || head->next == NULL)
return head;
Node *left=head;
Node *mid=middle(head);
Node *right=mid->next;
mid->next=NULL;
left=mergeSort(left);
right=mergeSort(right);
Node *mlist=merge(left,right);
return mlist;

}
};

// function which splits the circular linked list. head is pointer


// to head Node of given lined list. head1_ref1 and *head_ref2
// are pointers to head pointers of resultant two halves.

void splitList(Node *head, Node **head1_ref, Node **head2_ref)


{
// your code goes here
Node *p=head;
Node *slow=head;
Node *fast=head->next;
while(fast!=head && fast->next!=head)
{
slow=slow->next;
fast=fast->next->next;
}
while(p->next!=head)
{
p=p->next;
}

Node *p2=slow->next;
slow->next=head;
*head1_ref=head;
p->next=p2;
*head2_ref=p2;

}
Node *first=head;
Node *middle=NULL;
Node *trail=NULL;
bool flag=false;
while(first)
{
trail=middle;
middle=first;
first=first->next;
middle->next=trail;
}
first=middle;
while(first && head)
{
if(first->data==head->data)
{ flag=true;
first=first->next;
head=head->next;
}
else
{
flag=false;
break;
}

return flag;
//function to check palindrome or not
class Solution{
public:
//Function to check whether the list is palindrome.
bool isPalindrome(Node *head)
{
//Your code here
bool flag=false;
Node *first=head;
stack<int> s;
while(first)
{
s.push(first->data);
first=first->next;
}
while(head)
{
if(head->data==s.top())
{
flag=true;
s.pop();
head=head->next;
}
else
{
flag=false;
break;
}
}
return flag;
}
};
//reversal of dll
class Solution
{
public:
Node* reverseDLL(Node * head)
{
//Your code here
Node *temp=head;
Node *t=NULL;
if(temp->next==NULL)
{
return head;
}
while(temp)
{
t=temp->prev;
temp->prev=temp->next;
temp->next=t;
temp=temp->prev;
}
head=t->prev;
return head;
}
};
//heap sort
#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the
index of root node in array a[], and 'n' is the size of heap. */
void heapify(int a[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
// If left child is larger than root
if (left < n && a[left] > a[largest])
largest = left;
// If right child is larger than root
if (right < n && a[right] > a[largest])
largest = right;
// If root is not largest
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;

heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}

}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
//segrating the even and odd numbers of a linked list
vector<int> ve;
vector<int> vo;
Node *p=head;
while(p)
{
if((p->data)%2==0)
{
ve.push_back(p->data);
}
else
{
vo.push_back(p->data);
}
p=p->next;
}
p=head;
for(int i:ve)
{
p->data=i;
p=p->next;
}
for(int i:vo)
{
p->data=i;
p=p->next;
}
return head;

You might also like