Error while reading input

How to pass input scanner is not working in my case

hello @himesh.sharma25

pls share ur code , i will check .

import java.util.*; public class Main { public static Node insert(int n, Scanner scanner){ Node head = null ; Node tail = null; int data = scanner.nextInt(); int index = 0; while(index < n) { Node newNode = new Node<>(data); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; tail = tail.next; } data = scanner.nextInt(); index++; } return head; } public static void printLL(Node head){ while (head != null){ System.out.print(head.data + " "); head=head.next; } } public static void main (String args[]) { Scanner s = new Scanner(System.in); int size = s.nextInt(); Node head = insert(size, s); printLL(oddEven(head)); } private static Node oddEven(Node head){ if(head == null){ return null; } Node even = head.next, odd = head, evenHead = even; while(even != null && even.next != null){ odd.next = even.next; odd = odd.next; even.next = odd.next; even = even.next; } odd.next = evenHead; return head; } } class Node { Integer data; Node next; Node(Integer data) { this.data = data; } }

check now->

u were reading n+1 input …

also always share ur code using cb ide.

thanks its working now

Its giving wrong output cant think of scenarios of which it is failing

try this case->
6
2 1 3 4 5 6

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.