

【解题思路】
这道题比较简单,依次找链表中的节点比较,第一个相等的节点即为相交的节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p = headA, q;
while(p != null)
{
q = headB;
while(q != null)
{
if(p == q) return p;
q = q.next;
}
p = p.next;
}
return null;
}
}
这篇博客介绍了一种简单的算法,用于查找两个单链表的交点。通过同时遍历两个链表,当其中一个链表的节点为空时,另一个链表继续前进,直到两个链表的节点相遇,即可找到交点。这种方法避免了预先计算链表长度的步骤,提高了效率。
317

被折叠的 条评论
为什么被折叠?



