Advertisement
Martina312

Zero Consecutive Nodes

Feb 11th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //Ovaa verzija gi brishe samo sprotivnite nodes. Primer : 2 i -2, 3 i -3
  4. public class ZeroConsecutiveNodes {
  5.     public static void remove(SLL<Integer> list){
  6.         SLLNode<Integer> tmp=list.getFirst();
  7.         SLLNode<Integer> tmp2;
  8.  
  9.         while (tmp!=null){
  10.             tmp2=tmp.succ;
  11.             if(tmp2==null) break;
  12.  
  13.             while (tmp2!=null){
  14.                 if(tmp.element + tmp2.element == 0){
  15.                     list.delete(tmp2);
  16.                     SLLNode<Integer> kopija=tmp;
  17.                     list.delete(tmp);
  18.                     tmp=kopija.succ;
  19.                     break;
  20.                 }else{
  21.                     tmp2 = tmp2.succ;
  22.                 }
  23.             }
  24.  
  25.             if(tmp2==null){
  26.                 tmp=tmp.succ;
  27.             }
  28.         }
  29.         System.out.println(list);
  30.     }
  31.     public static void main(String[] args) {
  32.         Scanner in=new Scanner(System.in);
  33.         int n=in.nextInt();
  34.  
  35.         SLL<Integer> list=new SLL<>();
  36.         for(int i=0;i<n;i++){
  37.             list.insertLast(in.nextInt());
  38.         }
  39.  
  40.         remove(list);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement