Advertisement
Martina312

Next Greater Node

Feb 11th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class NextGreaterNode {
  4.     public static void greaterNodes(SLL<Integer> list){
  5.         SLLNode<Integer> tmp=list.getFirst();
  6.         SLLNode<Integer> tmp2;
  7.  
  8.         while (tmp!=null){
  9.             tmp2=tmp.succ;
  10.  
  11.             while (tmp2!=null){
  12.                 if(tmp2.element>tmp.element){
  13.                     System.out.print(tmp2.element+" ");
  14.                         break;
  15.                 }
  16.                 tmp2=tmp2.succ;
  17.             }
  18.             if(tmp2==null)
  19.             System.out.print(0+" ");
  20.             tmp=tmp.succ;
  21.         }
  22.     }
  23.     public static void main(String[] args) {
  24.         Scanner in=new Scanner(System.in);
  25.         int n=in.nextInt();
  26.  
  27.         SLL<Integer> list=new SLL<>();
  28.         for(int i=0;i<n;i++){
  29.             list.insertLast(in.nextInt());
  30.         }
  31.         greaterNodes(list);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement