-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
50 lines (50 loc) · 1.01 KB
/
LinkedList.java
File metadata and controls
50 lines (50 loc) · 1.01 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
import java.util.*;
public class LinkedList
{
static class Node
{
int data;
Node next;
Node(int val){
data=val;
}
}
static Node remDup(Node head)
{
Node curr=head;
while(curr!=null && curr.next!=null)
{
if(curr.data==curr.next.data)
curr.next=curr.next.next;
else
curr=curr.next;
}
return head;
}
static void print(Node head)
{
while(head!=null)
{
System.out.print(head.data+" ");
head=head.next;
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String inp[]=in.nextLine().trim().split(" ");
if(inp.length==0||inp[0].equals(""))
{
System.out.println("Empty list");
return;
}
Node head=new Node(Integer.parseInt(inp[0]));
Node curr=head;
for(int i=1;i<inp.length;i++){
curr.next=new Node(Integer.parseInt(inp[i]));
curr=curr.next;
}
head=remDup(head);
print(head);
}
}