Java remove duplicates from linked list

后端 未结 5 1643
眼角桃花
眼角桃花 2021-01-27 14:20

I want to remove duplicates from sorted linked list {0 1 2 2 3 3 4 5}.

`

public Node removeDuplicates(Node header)
{
    Node tempHeader = null;
    if(         


        
相关标签:
5条回答
  • 2021-01-27 14:43

    I can give you 2 suggestions for the above suggestion 1) Convert the linked List to Set, that will eliminate the duplicates and Back from Set to the Linked list Code to get this done would be

    linkedList = new LinkedList<anything>(new HashSet<anything>(origList));
    

    2) You can use LinkedHashSet, if you dont want any duplicates

    0 讨论(0)
  • 2021-01-27 14:43
    public ListNode removeDuplicateElements(ListNode head) {
      if (head == null || head.next == null) {
            return null;
        }
        if (head.data.equals(head.next.data)) {
            ListNode next_next = head.next.next;
            head.next = null;
            head.next = next_next;
            removeDuplicateElements(head);
        } else {
            removeDuplicateElements(head.next);
        }
        return head;
    }
    
    0 讨论(0)
  • 2021-01-27 14:45

    In this case no return value is needed.

    public void removeDuplicates(Node list) {
        while (list != null) {
            // Walk to next unequal node:
            Node current = list.next;
            while (current != null && current.data.equals(list.data)) {
                current = current.next;
            }
            // Skip the equal nodes:
            list.next = current;
            // Take the next unequal node:
            list = current;
    
        }
    }
    
    0 讨论(0)
  • 2021-01-27 14:55

    The loop is sorted, so you know that duplicates are going to sit next to each other. If you want to edit the list in place then, you've got to have two list pointers (which you do). The one you call tempHeader and prev, and you've got to advance them both in the the list as you go (which I don't see in the code). Otherwise, if you don't advance the prev pointer as you go, then you're always comparing the element under tempHeader to the first item in the list, which is not correct.

    An easier way to do this, however, is to build a new list as you go. Simply remember the value of the last item that you appended to the list. Then if the one that you're about to insert is the same then simply don't insert it, and when you're done, just return your new list.

    0 讨论(0)
  • 2021-01-27 14:58

    By DoublyLinked List and using HashSet,

      public static void deleteDups(Node n) {
        HashSet<Integer> set = new HashSet<Integer>();
        Node previous = null;
        while (n != null) {
            if (set.contains(n.data)) {
                previous.next = n.next;
            } else {
                set.add(n.data);
                previous = n;
            }
            n = n.next;
        }
    }
    

    doublylinkedList

    class Node{
    
    public Node next;
    public Node prev;
    public Node last;
    public int data;
    public Node (int d, Node n, Node p) {
        data = d;
        setNext(n);
        setPrevious(p);
    }
    
    public Node() { }
    
    public void setNext(Node n) {
        next = n;
        if (this == last) {
            last = n;
        }
        if (n != null && n.prev != this) {
            n.setPrevious(this);
        }
    }
    
    public void setPrevious(Node p) {
        prev = p;
        if (p != null && p.next != this) {
            p.setNext(this);
        }
    }}
    
    0 讨论(0)
提交回复
热议问题