DSAa
DSAa
{
// 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
class Solution
{
public:
struct node *reverse (struct node *head, int k)
{
if(head == NULL || head->next == NULL){
return head;
}
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 *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;