-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
70 lines (68 loc) · 1.42 KB
/
Node.java
File metadata and controls
70 lines (68 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
public class Main{
Node head;
void add(int data)
{
Node newNode = new Node(data);
if(head==null)
{
head=newNode;
}
else{
Node temp=head;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=newNode;
}
}
void display()
{
if(head==null)
{
System.out.print("list is empty!");
}
else
{
Node temp = head;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp=temp.next;
}
}
}
void removeDuplicates()
{
Node temp = head;
if(temp.data==temp.next.data)
{
temp.next=temp.next.next;
}
else{
temp=temp.next;
}
}
public static void main(String[] args){
Main list = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
int data=sc.nextInt();
list.add(data);
}
list.display();
System.out.println();
list.removeDuplicates();
list.display();
}
}