Java remove duplicates from linked list

后端 未结 5 1722
眼角桃花
眼角桃花 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: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;
    
        }
    }
    

提交回复
热议问题