当前位置:网站首页>leetcode/两个链表的第一个重合节点
leetcode/两个链表的第一个重合节点
2022-08-10 11:36:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 023. 两个链表的第一个重合节点 * 给定两个单链表的头节点 headA 和 headB ,请找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。 */
public class Solution23 {
/** * 散列法 * 先统计headA于set中,再逐个遍历headB判断set中是否包含某个结点 */
public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
Set<ListNode> aSet = new HashSet<>();
ListNode pa = headA;
while (pa != null) {
aSet.add(pa);
pa = pa.next;
}
ListNode pb = headB;
while (pb != null) {
if (aSet.contains(pb)) {
return pb;
}
pb = pb.next;
}
return null;
}
/** * 双指针,指针交叉,速度相同的双指针要想相遇需要从同样的起点出发 * * 若 a长度等于b,pa pb指针速度一致 不用遍历c就能相遇 * 若 a长度大于b,pb走完自己的链表(b+c)时,pa也走了长度(b+c)还剩余a-b没走,此时让pb也走a-b。pa和pb都走完a-b后二者就走到了同样的起点往前走 */
public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
// 特殊情况处理
if (null == headA || null == headB) {
return null;
}
// 双指针
ListNode pa = headA;
ListNode pb = headB;
while (pa != pb) {
pa = pa == null ? headB : pa.next;
pb = pb == null ? headA : pb.next;
}
return pa;
}
public static void main(String[] args) {
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/3u1WK4/solution/liang-ge-lian-biao-de-di-yi-ge-zhong-he-0msfg/
来源:力扣(LeetCode)
边栏推荐
- LeetCode 109. Sorted Linked List Conversion Binary Search Tree
- 16、Pytorch Lightning入门
- LeetCode 24. Swap nodes in linked list pairwise
- LeetCode 362. Design Hit Counter
- 7、Instant-ngp
- Servlet---Solve the problem of Chinese garbled characters in post requests
- 一文详解 implementation api embed
- 第5章 虚拟存储器
- 【Redis】内存回收策略
- 可视化服务编排在金融APP中的实践
猜你喜欢

态路小课堂丨如何为CXP光模块选择光纤跳线?

APP automation testing practice based on UiAutomator2+PageObject mode

dedecms supports one-click import of Word content

gpu-admission 源码分析

Network sockets (UDP and TCP programming)

Since the media hot style title how to write?Taught you how to write the title

three.js模糊玻璃效果

一文详解 implementation api embed

ViT结构详解(附pytorch代码)

16. Getting Started with Pytorch Lightning
随机推荐
被面试官问到消息队列的丢失、重复与积压问题该如何回答
tommy's spell
Not just running away, but saving the guy who mishandled rm -rf /*
苹果逆势扩大iPhone 14系列备货,总量或达9500万部
Clicking Exercise - 64 Longest Harmonic Subsequences
Pulling drills - 56 Finding the right interval
Cannot find symbol log because lombok is not found
LeetCode 445. Adding Two Numbers II
It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
codevs 2370 小机房的树 (LCA)
So delicious!Since using this interface artifact, my team efficiency has increased by 60%!
Network sockets (UDP and TCP programming)
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
LeetCode 19. 删除链表的倒数第 N 个结点
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
HDU 4135:Co-prime (容斥原理)
SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
HDU 6040 Hints of sd0061 (技巧)