Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc= new Scanner(System.in);
- int n= sc.nextInt();
- Ll l1= new Ll();
- for(int i=0;i<n;i++)
- l1.insert(sc.nextInt());
- l1.removeDuplicates();
- l1.print();
- }
- }
- class Node
- {
- int val;
- Node next;
- Node(int val)
- {
- this.val=val;
- next=null;
- }
- }
- class Ll
- {
- Node head,curr;
- void insert(int val)
- {
- Node n= new Node(val);
- if(head==null)
- {
- head=n;
- curr=n;
- return;
- }
- curr.next=n;
- curr= curr.next;
- return;
- }
- void print()
- {
- if (head==null)
- return;
- Node temp=head;
- while(temp!=null ){
- System.out.print(temp.val+" ");
- temp=temp.next;
- }
- return;
- }
- void removeDuplicates()
- {
- if(head==null)
- return;
- Node temp=head;
- int last=-1;
- Node l=new Node(-1);
- Node ret=l;
- while(temp!=null){
- if(temp.val!=last){
- if(temp.next==null || temp.next.val!=temp.val){
- l.next=temp;
- l=temp;
- }
- }
- last=temp.val;
- temp=temp.next;
- }
- l.next=null;
- head=ret.next;
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement