the class is not same as the file saved. i think there is an error in the pre written code if not help me out
Compiler error ... LINKEDLIST class is public class
@shudhanshu20092001_af6f20d24c617008 As I can see that the code you have submitted is is giving you the wrong answer. So use this approach.
Intuition :
We can take two pointers before and after to keep track of the two linked lists as described above. These two pointers could be used two create two separate lists and then these lists could be combined to form the desired reformed list.
Algo :
- Take Two Fake_heads to take care of odd and even list.
- Put loop on the given LinkedList.
- If the node is odd add it to odd fake_ head list.
- If the node is Even add it to the even fake_head list.
- At the set connect both of the list together and set head to odd list.
Code
public void evenAfter() throws Exception {
Node Even_head = new Node(0, null);
Node Odd_head = new Node(0, null);
Node even = Even_head;
Node odd = Odd_head;
Node temp = this.head;
while(temp != null){
if(temp.data % 2 == 0){
even.next = temp;
even = even.next;
}else{
odd.next = temp;
odd = odd.next;
}
temp = temp.next;
}
odd.next = Even_head.next;
this.head = Odd_head.next;
this.tail = even;
this.tail.next = null;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.