题解 | #链表中环的入口结点# 自定义Hash

链表中环的入口结点

https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4

import java.util.*;
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    // 计算哈希值
    public int computedHash(ListNode node) {
        int hash = 0;
        if (node != null) {
            hash += (node.val << 14);
        }
        if (node.next != null) {
            hash  = node.next.val << 7 ^ hash;
        }
        System.out.println(node.val);
        System.out.println(hash);
        return hash;
    }

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        HashMap<Integer, ListNode> hmap = new HashMap<>();
        while (pHead != null) {
            if (hmap.containsKey(computedHash(pHead))) {
                return hmap.get(computedHash(pHead));
            }
            hmap.put(computedHash(pHead), pHead);
            pHead = pHead.next;
        }
        return null;
    }
}

有更好的哈希计算方法,我这里只是单纯为了好写,能跑通就是成功!

全部评论

相关推荐

2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
01-30 10:21
中北大学 golang
27届学院本誓死冲击...:技术栈不用搞这么详细,主要还是看实习和项目,毕竟技术栈就那点,你能写这么多,别人也可以写这么多,模版换一下换成上下的,不知道怎么排版可以去看看别人怎么写
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务