Skip to content

Commit 00db47c

Browse files
Create 83. Remove Duplicates from Sorted List.java
1 parent 5be3c73 commit 00db47c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Three pointer approach that we use in in-place reversal of linked list.
2+
3+
class Solution {
4+
public ListNode deleteDuplicates(ListNode head) {
5+
ListNode p = null;
6+
ListNode q = null;
7+
ListNode r = head;
8+
while (r!=null) {
9+
if (q!=null && q.val == r.val) {
10+
r = r.next;
11+
q.next = r;
12+
}else {
13+
p = q;
14+
q = r;
15+
r = r.next;
16+
}
17+
}
18+
return head;
19+
}
20+
}

0 commit comments

Comments
 (0)