力扣hot100 相交链表 思维题

Problem: 160. 相交链表

文章目录

思路

👨‍🏫 参考题解

👩‍🏫 参考图解

复杂度

时间复杂度: O ( n + m ) O(n+m) O(n+m)

空间复杂度:

添加空间复杂度, 示例: O ( 1 ) O(1) O(1)

💖 Ac Code

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)
	{
		if (headA == null || headB == null)
			return null;
		ListNode you = headA;
		ListNode she = headB;
		while (you != she)
		{
//			设相交段为 C 则当走到相交段时 you ==she,不相交则走到 null
			you = you == null ? headB : you.next;// A + B + C
			she = she == null ? headA : she.next;// B + A + C
		}
		return you;
	}
}
相关推荐
nice_lcj5201 分钟前
排序(4)-归并排序专题——归并排序的分治美学
java·数据结构·算法·排序算法
洛水水16 分钟前
【力扣100题】83.最小栈
算法·leetcode·职场和发展
nice_lcj52025 分钟前
排序(3)-第三篇:交换排序专题——从冒泡排序到快速排序的效率飞跃
java·数据结构·算法·排序算法
ywl47081208730 分钟前
数据结构之链表反转算法
数据结构·算法·链表
牧子川31 分钟前
019-JSON-Schema-自动生成
算法·大模型·格式化输出·tools
lhjcsubupt41 分钟前
第二十二篇 从随机过程到IMU噪声模型
算法·机器学习·概率论
神仙别闹1 小时前
基于C语言处理机调度算法的实现
服务器·c语言·算法
Brilliantwxx1 小时前
【算法从零到千】【16-23】 二分算法
数据结构·算法
8Qi87 小时前
回文子串(Palindromic Substrings)—— 题解
算法·leetcode·职场和发展·动态规划