leetcode hot 01
1.leetcode hot 01
2.leetcode hot 023.leetcode hot 044.leetcode hot 035.leetcode hot 056.leetcode hot 067.leetcode hot 078.leetcode hot 089.leetcode hot 0910.leetcode hot 1011.leetcode hot 1212.leetcode hot 1313.leetcode hot 1414.leetcode hot 1515.leetcode hot 1116.leetcode hot 1617.leetcode hot 1718.leetcode hot 1819.leetcode hot 1920.leetcode hot 2021.leetcode hot 2122.leetcode hot 2223.leetcode hot 2324.leetcode hot 2425.leetcode hot 25解题思路:如果两个链表在某一点相交,那么那一点之后的node也都会相同,长度也相同。所以,我们先遍历获取对应每一条链表的长度,然后让长的链表先走两个链表长度之差的距离,然后再同时起步,每个节点进行对比,能不能找到相同的。
/**
* 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 dummy_head1 = new ListNode();
ListNode dummy_head2 = new ListNode();
dummy_head1.next = headA;
dummy_head2.next = headB;
ListNode p = dummy_head1;
ListNode q = dummy_head2;
int count1 = 0;
int count2 = 0;
while(p.next!=null){
count1++;
p = p.next;
}
while(q.next!=null) {
count2++;
q = q.next;
}
p = dummy_head1;
q = dummy_head2;
while(count1>count2)
{
p = p.next;
count1--;
}
while(count1<count2)
{
q = q.next;
count2--;
}
while(p.next!=null){
p = p.next;
q = q.next;
if(p == q){
return p;
}
}
return null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~