LC#83 : Remove Duplicates from Sorted List
Leet Code Two Pointers

25 minutes


go back go back go back home home

LC#83 : Remove Duplicates from Sorted List

Using 2 fast-slow pointers.

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && slow != null){
            if(slow.val != fast.val) {
                slow.next = fast;
                slow = fast;
            }
            if(slow.val == fast.val && fast.next == null){
                slow.next = null;
                return head;
            }
            fast = fast.next;
        }
        return head;
    }
}