why my all test case are not passing in this question through this code?
where am i going wrong?
EVEN-AFTER-ODD Doubt-Mohit
I am getting segmentation fault, i am not getting any approach how to debug this. Can you plz guide me on this? or can you tell me what corrections should i do?
It’s a little difficult to understand your exact approach, so please add some relevant comments to it.
Or, you can code the approach mentioned below:
You can take two different pointers denoting two linked lists:
- Even Linked List
- Odd Linked List
Then traverse through your original Linked List and whenever there is an even node add that to Even Linked List and if it’s an odd node then add it to Odd Linked List.
After completing the traversal of the list.
Add Even Linked List’s head at the end of Odd Linked List and then you can just return the Odd Linked List’s head.
For Eg.
Input:
7
1 2 4 3 8 5 6
Approach
Even Linked List: 2-> 4 ->8 -> 6
Odd Linked List: 1->3 -> 5
then,
Odd Linked List -> Even Linked List
i.e. 1->3 -> 5 ->2-> 4 ->8 -> 6
and return head of Odd Linked List
I have added comments to the code.
Also i am not making two different new linked list and finally adding them in last.
what am i doing is that i am transferring even data node to the last of the linked list.
Your approach is wrong.
Check the loop at line:55
while(curr->data%2==0 and curr!=end){//this loop will transfer all the nodes containing even data to last of linked list and it will run until we find odd node or it reach to last node
new_end->next=curr; //it will make the current node to connect with last node
curr=curr->next; // it will transfer the curr pointer to the next node
new_end->next->next=NULL;
new_end=new_end->next;
}
Let’s say your Linkedlist is 2 -> 3 ->5-> 4-> 7->NULL
then your loop will shift 2 at end like this 3 ->5-> 4-> 7->2->NULL
and you will never be able to traverse the linkedList using the head.
Let’s take another example where this approach will fail,
for linkedList 1->2->3->5->6->7>NULL
your loop will shift 2 at end without changing the 1’s next, so it will look like
3->5->6->7->2->NULL
1->2
In this case also you, will not be able to tarverse your linkedList further.
So, your this appproach will not work, please try the approach I have suggested earlier, that is more simple and would traverse the nodes also only once.
Both the examples given by you are running fine in my code.
The only case which is not running is
7
1 2 4 3 8 5 6
I am getting segmetation fault in this case. Where my code is doing wrong in this test case?
Hey @kalindiyadav5
These examples were just to tell you the source of the error. Both of them are working as their last number is an odd number. But your code would fail for the examples where last number is an even number and there are more than one even numbers in the given sequence
For eg.
Your code will work for this
4
1 2 6 3
But not for
3
1 2 6
and
4
1 2 3 6
I am really sorry ma’am i am giving you so much trouble. But when i dry run my code on these test cases i am getting correct output
. I request you to plz you also dry run. Also i reff