It is showing TLE error and is not passing any of the test cases and nor compiling.
Time Limit Exceeded
hey, can you share your code?
Posting this for help anyway
correct approach:
The idea is to split the linked list into two: one containing all even nodes and other containing all odd nodes. And finally attach the odd node linked list after the even node linked list.
To split the Linked List, traverse the original Linked List and move all odd nodes to a separate Linked List of all odd nodes. At the end of loop, the original list will have all the even nodes and the odd node list will have all the odd nodes. To keep the ordering of all nodes same, we must insert all the odd nodes at the end of the odd node list. And to do that in constant time, we must keep track of last pointer in the odd node list.
okay, follow this approach
take four pointers and initialise all to NULL
oddend and evenend to store the ends of even number and odd numbers
and oddstart and evenstart to store the beginning of them
check the first node if it is odd or even and point oddstart or evenstart accordingly
now for temp = head->next start running a loop
in the loop:
if an odd number is encountered and if oddstart is NULL point oddstart to it else if oddstart->next==NULL point odd end else point oddend->next to this temp and move oddend to it next
similarly follow the simlar steps for even numbers
at the end of the loop point oddend to even start and return oddstart as your new head
this is the most optimum one
another one u can follow is in this code
Thank you so much…but the code provided is only passing two of the test cases.
I tried posting the following code but it showed segmentation fault in line no 4
#include
#include<bits/stdc++.h>
using namespace std;
class node {
public:
int data;
node* next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
void display(node* head) {
node* temp = head;
while (temp != NULL) {
cout << temp->data <<" ";
temp = temp->next;
}
}
void insertAtHead(node* &head, int data) {
node* n = new node(data);
n->next = head;
head = n;
}
void insertAtTail(node* &head, int data) {
if (head == NULL) {
insertAtHead(head, data);
return;
}
node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
node* n = new node(data);
temp->next = n;
}
node* even_after_odd(node* head)
{
if(head==NULL or head->next==NULL)
{
return head;
}
node* evenend=NULL, *evenstart=NULL;
node* oddend=NULL, *oddstart=NULL;
if(head->data%2!=0)
{
oddstart==head;
oddstart->next=oddend;
}
else
{
evenstart=head;
evenstart->next=evenend;
}
node* temp=head->next;
while(temp!=NULL)
{
if(temp->data%2!=0)
{
if(oddstart==NULL)
{
oddstart=temp;
oddstart->next=oddend;
}
else{
oddend->next=temp;
}
}
else{
if(evenstart==NULL)
{
evenstart=temp;
evenstart->next=evenend;
}
else{
evenend->next=temp;
}
}
temp=temp->next;
}
head=oddstart;
oddend->next=evenstart;
evenend->next=NULL;
return head;
}
int main() {
int n,k;
cin>>n;
int a[n];
node* head=NULL;
for(int i=0;i<n;i++)
{
cin>>a[i];
insertAtTail(head,a[i]);
}
head=even_after_odd(head);
display(head);
return 0;
}
Hey, the code provided is just for reference, it does not match the input format either, u can look at the approach, the better one is to implement the one I wrote down
Ur code, please paste it onto the online ide and provide the link, it is hard to debug an unindented code here