zoukankan      html  css  js  c++  java
  • 剑指Offer——删除链表中重复的结点

    1、题目描述

      在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    2、代码实现

    import java.util.HashMap;
    public class Solution {
        public ListNode deleteDuplication(ListNode pHead)
        {
         //1、临界值的判断:
            if (pHead == null) {
                return null;
            }
            HashMap<Integer, Integer> hashMap = new HashMap<>();
            ListNode temp = pHead;
            while (temp != null) {
                if (hashMap.containsKey(temp.val) == true) {
                    hashMap.put(temp.val, hashMap.get(temp.val) + 1);
                } else {
                    hashMap.put(temp.val, 1);
                }
                temp = temp.next;
            }
            temp = pHead;
            ListNode newHead = new ListNode(-1);
            ListNode NTemp = newHead;
            while (temp != null) {
                if (hashMap.get(temp.val) > 1) {
                    temp = temp.next;
                } else {
                    NTemp.next = new ListNode(temp.val);
                    NTemp = NTemp.next;
                    temp = temp.next;
                }
            }
            return newHead.next;
        }
    }
    

      

  • 相关阅读:
    行转列
    multipath 安装配置
    网卡绑定
    numa对MySQL多实例性能影响
    Fatal NI connect error 12170
    REVOKE DBA权限要小心
    Oracle 数据库整理表碎片
    listagg 函数
    10046 事件补充
    tkprof 解释
  • 原文地址:https://www.cnblogs.com/BaoZiY/p/11183573.html
Copyright © 2011-2022 走看看