-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_delete_duplicate_nodes.java
More file actions
50 lines (39 loc) · 1.07 KB
/
Copy pathLinkedList_delete_duplicate_nodes.java
File metadata and controls
50 lines (39 loc) · 1.07 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
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
Node RemoveDuplicates(Node head) {
// This is a "method-only" submission.
// You only need to complete this method.
//special cases
//list is length one
//list is length zero
Node current = head;
Node previous = head;
boolean snd_node = false;
if(current == null) {
return head;
}
while(current != null) {
//if the two adjacent nodes are equal
if(current.data == previous.data) {
//if current is not on second node, just continue
if(!snd_node) {
current = current.next;
snd_node = true;
continue;
} else {
//otherwise, delete current node from linked list
previous.next = current.next;
current = current.next;
continue;
}
}
current = current.next;
previous = previous.next;
}
return head;
}