删除无序单链表中值重复出现的节点

【题目】
给定一个无序单链表的头节点head,删除其中值重复出现的节点。
例如:1->2->3->3->4->4->2->1->1->null,删除值重复的节点之后为1->2->3->4->null.
请按一下要求实现两种方法
1.如果链表长度为N,时间复杂度达到O(N)
2.额外空间复杂度为O(1)
【解析】
方法一:利用哈希表,时间复杂度为O(N),额外空间复杂度为O(N)
具体实现:
1.生成一个哈希表,因为头结点是不用删除的节点,所以将头节点值放入哈希表。
2.从头节点的下一个节点开始往后遍历节点,假设当前遍历到cur节点,先检查cur的值是不是在哈希表中,如果在,说明cur节点的值是之前出现过的,就将cur节点删除,删除的方式是将最近一个没有被删除的节点pre链接到cur的下一个节点,即pre.next = cur.next.如果不在,将cur节点的值加入哈希表,同时令pre=cur,即更新最近一个没有被删除的节点

package 删除无序链表中的值重复出现的节点;

import java.util.HashSet;

public class Main {

    
    /**节点类*/
    public static class Node{
        public int value;
        public Node next;
        
        public Node(int data){
            this.value = data;
        }
    }
    
    /**哈希表删除重复节点*/
    
    public static void removeRep1(Node head){
        if (head == null) {
            return;
        }
        HashSet<Integer> set = new HashSet<Integer>();
        Node pre = head;
        Node cur = head.next;
        set.add(head.value);
        
        while (cur != null) {
            if (set.contains(cur.value)) {
                pre.next = cur.next;
            } else {
                set.add(cur.value);
                pre = cur;
            }
            cur = cur.next;
        }
        
        
        
    }
    
    
    
    public static void main(String[] args) {
        Node head = new Node(1);
        Node head2 = new Node(2);
        Node head3 = new Node(3);
        Node head4 = new Node(3);
        Node head5 = new Node(5);
        Node head6 = new Node(5);
        Node head7 = new Node(5);
        Node head8 = new Node(5);
        head.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        head5.next = head6;
        head6.next = head7;
        head7.next = head8;
        removeRep1(head);
        print(head);
        
    }



    private static void print(Node head) {
        if (head == null) {
            return ;
        } 
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.value+"->");
            cur = cur.next;
        }
        System.out.println();
        
    }
    
    
    
}

方法二:


image.png
/**选择删除节点*/
    public static void removeRep2(Node head){
        Node cur = head;
        Node pre = null;
        Node next = null;
        while (cur != null) {
            pre = cur;
            next = cur.next;
            while (next != null) {
                if (cur.value == next.value) {
                    pre.next = next.next;
                } else {
                    pre = next;
                }
                next = next.next;
            }
            cur = cur.next;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是数组? 数组简单来说就是将所有的数据排成一排存放在系统分配的一个内存块上,通过使用特定元素的索引作为数组的下...
    启明_b56f阅读 995评论 0 0
  • 像一条柔韧的绳子,情这个字,不知勒痛多少人的心肉。。。
    慕容思博阅读 226评论 1 4
  • 失眠,不舍 迷茫,压力 果然人越来越大,就越容易伤春悲秋。这个学期明显感觉到自己对很多事情都力不从心或者没有那么多...
    宝贝微笑nice阅读 174评论 0 0