-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDuplicates.java
38 lines (35 loc) · 1.22 KB
/
RemoveDuplicates.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
/*
# Problem Statement: remove the duplicate elements from a given linked list
---------------------------------------------------------------
# Approach :
- traverse untill the list is empty
- chacke for the pointers value and move the pointers accordingly
---------------------------------------------------------------
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return null;
}
ListNode temp1, temp2;
temp1 = head;
temp2 = head.next;
int last = head.val;
while(temp2 != null){ // while end of Linked list
if(temp2.val == last){ // Current number same to last number
if(temp2.next == null){ // If last element, just delete and break loop
temp1.next = null;
break;
}
temp2 = temp2.next; // Not last, then delete that element
temp1.next = temp2; // and move to next element
}
else{ // If not the same as last element, jump to next node
temp1 = temp2;
last = temp1.val;
temp2 = temp2.next;
}
}
return head;
}
}