Advertisement
unknown_0711

Untitled

Oct 6th, 2022
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  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. Scanner sc= new Scanner(System.in);
  10. int n= sc.nextInt();
  11. Ll l1= new Ll();
  12. for(int i=0;i<n;i++)
  13. l1.insert(sc.nextInt());
  14. l1.removeDuplicates();
  15. l1.print();
  16. }
  17. }
  18. class Node
  19. {
  20. int val;
  21. Node next;
  22. Node(int val)
  23. {
  24. this.val=val;
  25. next=null;
  26. }
  27. }
  28. class Ll
  29. {
  30. Node head,curr;
  31. void insert(int val)
  32. {
  33. Node n= new Node(val);
  34. if(head==null)
  35. {
  36. head=n;
  37. curr=n;
  38. return;
  39. }
  40. curr.next=n;
  41. curr= curr.next;
  42. return;
  43. }
  44. void print()
  45. {
  46. if (head==null)
  47. return;
  48. Node temp=head;
  49. while(temp!=null ){
  50. System.out.print(temp.val+" ");
  51. temp=temp.next;
  52. }
  53. return;
  54. }
  55. void removeDuplicates()
  56. {
  57. if(head==null)
  58. return;
  59. Node temp=head;
  60. int last=-1;
  61. Node l=new Node(-1);
  62. Node ret=l;
  63. while(temp!=null){
  64. if(temp.val!=last){
  65. if(temp.next==null || temp.next.val!=temp.val){
  66. l.next=temp;
  67. l=temp;
  68. }
  69. }
  70. last=temp.val;
  71. temp=temp.next;
  72. }
  73. l.next=null;
  74. head=ret.next;
  75. return;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement