Q19 Remove Duplicates from Sorted List - LeetCode

PHOTO EMBED

Wed Jan 18 2023 05:52:08 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        
        ListNode dummyhead = head ;
        ListNode dummy = dummyhead;
        ListNode curr = head.next;

        while(curr!= null){
            if(curr.val != dummy.val){
                dummy.next = curr;
                dummy = dummy.next;
            }
            curr=curr.next;
        }
        dummy.next = curr;  //setting last node.next to null
        return dummyhead;

    }
}




content_copyCOPY

https://leetcode.com/problems/remove-duplicates-from-sorted-list/