算法速刷(LeetCode)(160.相交链表)

个人思路:

笨蛋做法,但是好用

代码如下

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        
       Set set = new HashSet();
       ListNode  temp = headA;
       while(temp != null) {
        set.add(temp);
        temp = temp.next;
       }

       temp = headB;
       while(temp != null) {
            if(set.contains(temp)){
                return temp;
            }
            temp = temp.next;

       }
        return null;

    }
}
相关推荐
短剑重铸之日2 小时前
7天读懂MySQL|Day 5:执行引擎与SQL优化
java·数据库·sql·mysql·架构
酒九鸠玖2 小时前
Java--多线程
java
Dreamboat-L2 小时前
云服务器上部署nginx
java·服务器·nginx
长安er2 小时前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓2 小时前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
Java后端的Ai之路3 小时前
【神经网络基础】-神经网络学习全过程(大白话版)
人工智能·深度学习·神经网络·学习
小白菜又菜3 小时前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
粉红色回忆3 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++
cici158743 小时前
C#实现三菱PLC通信
java·网络·c#
登山人在路上4 小时前
Nginx三种会话保持算法对比
算法·哈希算法·散列表