diff --git a/java/83. Remove Duplicates from Sorted List.java b/java/83. Remove Duplicates from Sorted List.java new file mode 100644 index 000000000..e7362e299 --- /dev/null +++ b/java/83. Remove Duplicates from Sorted List.java @@ -0,0 +1,20 @@ +//Three pointer approach that we use in in-place reversal of linked list. + +class Solution { + public ListNode deleteDuplicates(ListNode head) { + ListNode p = null; + ListNode q = null; + ListNode r = head; + while (r!=null) { + if (q!=null && q.val == r.val) { + r = r.next; + q.next = r; + }else { + p = q; + q = r; + r = r.next; + } + } + return head; + } +}