Java remove duplicates from linked list

后端 未结 5 1693
眼角桃花
眼角桃花 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

    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;
    }
    

提交回复
热议问题