This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveLinkedListDuplicates.java
104 lines (87 loc) · 2.71 KB
/
RemoveLinkedListDuplicates.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.ematrix;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
public class RemoveLinkedListDuplicates {
public void removeDuplicates(List S){
if(S.isEmpty()){return;}
for(int i=0;i<S.size();i++){
int data =(int)S.get(i);
for(int j=i+1;j<S.size();j++){
if(data==(int)S.get(j)){
S.remove(j);
}
}
}
}
static class node
{
int val;
node next;
public node(int val)
{
this.val = val;
}
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
public void deleteDups2(node head) {
node current = head;
while (current != null) {
/* Remove all future nodes that have the same value */
node runner = current;
while (runner.next != null) {
if (runner.next.val == current.val) {
runner.next = runner.next.next;
} else {
runner = runner.next;
}
}
current=current.next;
}
}
public static void main(String[] args){
LinkedList list =new LinkedList();
list.push(10);
list.push(12);
list.push(10);
list.push(12);
list.push(8);
list.push(1);
list.push(34);
RemoveLinkedListDuplicates removeLinkedListDuplicates=new RemoveLinkedListDuplicates();
removeLinkedListDuplicates.removeDuplicates(list);
for(Object item:list){
System.out.print(item+"\n");}
/* The constructed linked list is:
10->12->11->11->12->11->10*/
node start = new node(10);
start.next = new node(12);
start.next.next = new node(11);
start.next.next.next = new node(11);
start.next.next.next.next = new node(12);
start.next.next.next.next.next = new node(11);
start.next.next.next.next.next.next = new node(10);
System.out.println("Linked list before removing duplicates :");
}
}