【牛客-剑指offer-数据结构篇】【图解】JZ76 删除链表中重复的结点 两种思路 Java实现


1 题目链接

https://www.nowcoder.com/exam/oj/ta?page=1&tpId=13&type=13

2 题目

在这里插入图片描述
在这里插入图片描述

3 思路 & 代码

方案一 LinkedHashMap,创建新链表

  1. 用LinkedHashMap来统计每一个数字出现的次数
  2. 遍历LinkedHashMap,找出那些出现次数为1的值
  3. 创建一个新的链表
import java.util.*;
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null) {
            return null;
        }
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
        ListNode tmp = pHead;
        while (tmp != null) {
            if (!map.containsKey(tmp.val)) {
                map.put(tmp.val, 1);
            } else {
                map.put(tmp.val, map.get(tmp.val) + 1);
            }
            tmp = tmp.next;
        }

        Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
        ListNode pre = new ListNode(-1);
        pHead = pre;
        while (iterator.hasNext()) {
            Map.Entry<Integer, Integer> entry = iterator.next();
            if (entry.getValue() == 1) {
                ListNode node = new ListNode(entry.getKey());
                pre.next = node;
                pre = node;
            }
        }
        return pHead.next;

    }
}

方案二 HashMap,删除原链表中的节点

在这里插入图片描述

在这里插入图片描述

  1. 用HashMap来统计每一个数字出现的次数
  2. 遍历链表,以当前节点的值为key,检查对应的value值是否大于1
    • 如果大于1,则tmp指针向后移动value次,然后pre.next指向tmp
    • 如果等于1,pre和tmp同时向后移动一位
    • 在移动以后,如果tmp为null,或者tmp.next为null,则结束循环,返回头节点
import java.util.*;
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {

    public ListNode deleteDuplication(ListNode pHead) {
        //链表为空
        if (pHead == null) {
            return null;
        }

        HashMap<Integer, Integer> map = new HashMap<>();
        ListNode tmp = pHead;
        while (tmp != null) {
            if (!map.containsKey(tmp.val)) {
                map.put(tmp.val, 1);
            } else {
                map.put(tmp.val, map.get(tmp.val) + 1);
            }
            tmp = tmp.next;
        }

        ListNode pre = new ListNode(-1);
        ListNode head = pre;
        pre.next = pHead;
        tmp = pHead;

        int count;
        while (tmp != null && tmp.next != null) {
            //获取当前这个值在链表中出现几次
            count = map.get(tmp.val);
            if (count > 1) {
                //该节点及其相同节点需要删除
                while (count > 0) {
                    tmp = tmp.next;
                    count--;
                }
                //删除重复的节点
                pre.next = tmp;
            } else {
                pre = tmp;
                tmp = tmp.next;
            }
        }

        return head.next;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值