Problem: 160. 相交链表
文章目录
- 思路
- 复杂度
- 💖 Ac Code
思路
👨🏫 参考题解
👩🏫 参考图解
复杂度
时间复杂度: O ( n + m ) O(n+m) O(n+m)
空间复杂度:
添加空间复杂度, 示例: O ( 1 ) O(1) O(1)
💖 Ac Code
/*** 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,不相交则走到 nullyou = you == null ? headB : you.next;// A + B + Cshe = she == null ? headA : she.next;// B + A + C}return you;}
}