The same code passes the leetcode test-cases successfully but not here.
Can you please look into the code?
Whats wrong with the code
hello @apu_2017054 i would like to help you in the better way for that i request you if you can please share your code and please tell the question name .
import java.util.*;
public class Main {
static class Node{
int val;
Node next;
Node (int val){
this.val = val;
next = null;
}
}
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = input.nextInt();
Node head = null;
for(int i=0;i<n;i++)
head = insert(head,a[i]);
head = oddEvenSort(head,n);
printList(head);
}
static Node oddEvenSort(Node head,int n){
if(head == null)
return null;
int i = 1;
Node odd = null;
Node even = null;
Node current = head;
while(i<=n){
if(i%2!=0){
odd = insert(odd,current.val);
}
else{
even = insert(even,current.val);
}
current = current.next;
i++;
}
current = odd;
while(current.next!=null)
current = current.next;
current.next = even;
head = odd;
return head;
}
static Node insert(Node head,int k){
Node temp = new Node(k);
if(head == null){
head = temp;
head.next = null;
return head;
}
Node current = head;
while(current.next!=null)
current = current.next;
current.next = temp;
current = current.next;
current.next = null;
return head;
}
static void printList(Node head){
Node current = head;
while(current!=null){
System.out.print(current.val+" ");
current = current.next;
}
//System.out.println();
}
} indent preformatted text by 4 spaces
you have taken course in java language ?
Yes, I had in my second year.
@apu_2017054 please share your code by saving it on ide.codingblocks.com
and i am asking whether you are doing the course in java language ?