Preview:
/**
 * 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;

    }
}




downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter