Intersection of Two Linked Lists找2个链表的交合点

给你2个链表,需要找到这2个链表的重合点的起点,从起点之后所有的点都一样了。这里题目要求时间

复杂度o(n),空间复杂度o(1),就说明不需要额外的空间,只需要遍历就能做出来,关键是思路的问题。

然后就以这个o(n)和o(1)为前提进行思考,首先想到的就是长度,重叠之后的元素都一样了,遍历到

最后一个肯定是一样的,否则就不重叠。然后是怎么找这个重叠的点,然后也好像只能从长度下手了,

因为后面的长度都一样,那前面的长度的差距就是总长度的差距,然后就有了思路了。

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL)return NULL;
ListNode *headA_t = headA;
ListNode *headB_t = headB;
int lenA = 0;
int lenB = 0;
while(headA_t != NULL)
{
headA_t = headA_t->next;
lenA++;
}
while(headB_t != NULL)
{
headB_t = headB_t->next;
lenB++;
}
if(headA_t != headB_t)
{
return NULL;
}
if(lenA <= lenB)
{
while(lenA < lenB)
{
headB = headB->next;
lenA++;
}
}else
{
while(lenB < lenA)
{
headA = headA->next;
lenB++;
}
}
while(headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA;
}

唉,老写算法没意思,过几天写个渗透的文章,看看有没有有趣点的网站可以弄弄。

2015/5/9   by  梅开二度

时间: 2024-10-18 09:09:24

Intersection of Two Linked Lists找2个链表的交合点的相关文章

LeetCode—Intersection of Two Linked Lists 找两个链表的相交位置,让长的链表先走一段

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[LintCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. Notice If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. Y

160. Intersection of Two Linked Lists(找出两个链表的交点)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链

[LeetCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

leetCode 160. Intersection of Two Linked Lists 链表

160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A:          a1 → a2                                          c1 → c2 → c3

[LeetCode] 160. Intersection of Two Linked Lists 解题思路

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

leetcode_160题——Intersection of Two Linked Lists(线性表,哈希表)

Intersection of Two Linked Lists Total Accepted: 28581 Total Submissions: 100989My Submissions Question Solution Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists

【leetcode】Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: