Given a singly linked list, delete middle node of the linked list. If there are even nodes, then there would be two middle nodes, we need to delete the second middle element.

After deleting the node, print the linked list.

In case of a single node, print the head of a linked list containing only 1 node which has value -1.

#### Input

The first line contains an integer n, the length of the linked list.

The next line contains n integers, the elements of the linked list.

#### Constraints:

1 <= n <= 1000

1 <= list[i] <= 1000, where list[i] is the ith element of the list.

#### Output

Print the linked list after deleting the middle node in new line.

### Example

#### Input:

5 1 2 3 4 5

#### Output:

1 2 4 5

#### Explanation:

After deleting middle of the linked list, 3 will be deleted and the list will be as 1, 2, 4, 5.

#### Input:

6 1 2 3 4 5 6

#### Output:

1 2 3 5 6

#### Explanation:

After deleting middle of the linked list, 4 will be deleted and the list will be as 1, 2, 3, 5, 6.

## Solution of Delete the Middle of the Linked List:–

import java.util.*; import java.lang.*; import java.io.*; public class Main { class Node{ int data; Node next; Node(int x) { data=x; next=null; } } Node head; void addLast(int x) { Node newNode = new Node(x); if(head==null) { head=newNode; return; } Node temp=head; while(temp.next!=null) { temp=temp.next; } temp.next=newNode; return; } void deleteMiddle(int n) { if(n==0) { head=head.next; return; } if(head==null) { return; } if(head.next==null) { return; } int i=0; Node temp=head; Node demo=null; while(i<(n/2) && temp.next!=null) { demo=temp; temp=temp.next; i++; } demo.next=temp.next; return; } void printList() { if(head.next==null) { System.out.println(-1); return; } Node temp=head; while(temp!=null) { System.out.print(temp.data+" "); temp=temp.next; } } public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Main m = new Main(); for(int i=0;i<n;i++) { int value=sc.nextInt(); m.addLast(value); } m.deleteMiddle(n); m.printList(); } }

## Add a Comment