Facebook
From Emerald Capybara, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 140
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7.         public static void main (String[] args) throws java.lang.Exception
  8.         {
  9.                 //your code here
  10.                 Scanner sc=new Scanner(System.in);
  11.                 int n=sc.nextInt();
  12.                 int k=sc.nextInt();
  13.                 linkedList l=new linkedList();
  14.                 for(int i=0;i<n;i++){
  15.                         l.add(sc.nextInt());
  16.                 }
  17.                 l.print();
  18.                 System.out.println();
  19.                 // l.printReverse();
  20.         }
  21. }
  22. class linkedList{
  23.         Node head=null;
  24.         Node tail=null;
  25.         public void add(int val){
  26.                 Node newNode=new Node(val);
  27.                 if(head==null){
  28.                         head=newNode;
  29.                         tail=newNode;
  30.                         head.next=null;
  31.                         tail.prev=null;
  32.                 }
  33.                 tail.next=newNode;
  34.                 newNode.prev=tail;
  35.                 tail=newNode;
  36.         }
  37.         public void print(){
  38.                 Node temp=head;
  39.                 while(temp!=null){
  40.                         System.out.print(temp.val+" ");
  41.                         temp=temp.next;
  42.                 }
  43.         }
  44.         public void printReverse(){
  45.                 Node temp=tail;
  46.                 while(temp.prev!=null){
  47.                         System.out.print(temp.val+" ");
  48.                         temp=temp.prev;
  49.                 }
  50.         }
  51. }
  52. class Node{
  53.         int val;
  54.         Node next;
  55.         Node prev;
  56.         Node(int val){
  57.                 this.val=val;
  58.                 this.next=null;
  59.                 this.prev=null;
  60.         }
  61. }