Repository navigation
Expand file tree
/
Copy pathLinkedListNode.java
More file actions
67 lines (57 loc) · 1.69 KB
/
Copy pathLinkedListNode.java
File metadata and controls
67 lines (57 loc) · 1.69 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
import java.util.HashSet;
import java.util.Set;
// This the basic linked list representation
public class LinkedListNode {
private LinkedListNode next;
private int data;
LinkedListNode(int data){
this.data = data;
this.next = null;
}
public void addNode(int data){
LinkedListNode newNode = new LinkedListNode(data);
LinkedListNode head = this;
while (head.next != null){
head = head.next;
}
head.next = newNode;
}
public void removeNode(int data){
LinkedListNode node = this;
if (node.data == data){
node = node.next;
return;
}
while (node.next != null){
if (node.next.data == data){
node.next = node.next.next;
return;
}
node = node.next;
}
}
public static void print(LinkedListNode head){
LinkedListNode node = head;
System.out.print(node.data + "->");
while(node.next != null){
node = node.next;
System.out.print(node.data + "->");
}
System.out.println();
}
// [Easy] Stmt: Find the duplicate values in unsorted Linked List
public static void removeDuplicateInLinkedList(LinkedListNode linkedList) {
Set<Integer>track = new HashSet<>();
LinkedListNode head = linkedList;
LinkedListNode previous = null;
while(head.next != null){
if (track.contains(head.data)){
previous.next = head.next;
} else {
track.add(head.data);
previous = head;
}
head = head.next;
}
}
}