Advertisement
unknown_0711

Untitled

Jul 5th, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. public class Main{
  5.     static Node head;
  6.     static class Node{
  7.         int data;
  8.         Node next;
  9.  
  10.         public Node(int data){
  11.             this.data = data;
  12.             this.next = null;
  13.         }
  14.     }
  15.     public static void printFunction(){
  16.       if(head == null){
  17.         System.out.print("Linked List is Empty");
  18.       }
  19.       Node temp = head;
  20.       while(temp != null){
  21.         System.out.print(temp.data + " ");
  22.         temp = temp.next;
  23.       }
  24.       System.out.println();
  25.     }
  26.     public static void main (String[] args) throws java.lang.Exception{
  27.         Scanner sc = new Scanner(System.in);
  28.         int n = sc.nextInt();
  29.          head = new Node(0);
  30.         Node tmp = head;
  31.         for(int i=0;i<n;i++){
  32.             int data = sc.nextInt();
  33.             Node node  = new Node(data);
  34.             tmp.next = node;
  35.             tmp = tmp.next;
  36.         }
  37.         int position = sc.nextInt();
  38.         head = head.next;
  39.         if(head == null) return;
  40.         Node curr = head;
  41.         int count = 0;
  42.         while(count < position){
  43.             curr = curr.next;
  44.             count++;
  45.         }
  46.         Node temp = curr.next;
  47.         curr.next = curr.next.next;
  48.         temp.next = null;
  49.        
  50.         printFunction();
  51.     }      
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement