LeetCode – Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

Thoughts

The key of this problem is using the right loop condition. And change what is necessary in each loop. You can use different iteration conditions like the following 2 solutions.

Solution 1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)
            return head;
 
        ListNode prev = head;    
        ListNode p = head.next;
 
        while(p != null){
            if(p.val == prev.val){
                prev.next = p.next;
                p = p.next;
                //no change prev
            }else{
                prev = p;
                p = p.next; 
            }
        }
 
        return head;
    }
}

Solution 2

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)
            return head;
 
        ListNode p = head;
 
        while( p!= null && p.next != null){
            if(p.val == p.next.val){
                p.next = p.next.next;
            }else{
                p = p.next; 
            }
        }
 
        return head;
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Aiden Zou

    yes. because the deleted node was no longer reachable by any instance.

  • kumud883

    malaji…. need to implement LINKLIST….. not with this easy shit which u pasted.. lolzzzzz

  • Amrut Malaji

    public class RemoveDuplicate {

    public static void main(String[] args) {

    Collection a = new LinkedList();

    a.add(5);

    a.add(5);

    a.add(9);

    a.add(20);

    a.add(20);

    a.add(65);

    a.add(70);

    a.add(70);

    System.out.println(a);

    Collection b = new TreeSet(a);

    System.out.println(b);

    }

    }

  • RonRdRd

    Try this solutions !

    public Node removeDup(Node node) {

    if(node == null) {

    return node;

    }

    Node temp = removeDup(node.next);

    if(temp != null) {

    if(node.val == temp.val) {

    return temp;

    } else {

    node.next = temp;

    }

    }

    return node;

    }

  • Anon

    if head is null, then head.next == null wont be evaluated since the first part of the OR was already true.

  • acewings

    for both solutions, if head = null, then wouldn’t the if statement at the top throw a NullPointerException when you try to run if(head==null || head.next==null), since you can’t do null.next in java?

  • voldemort

    so does the deleted node object get garbage collected after ?