**
以下为C++版本
**
已知链表A的头节点指针headA,链表B的头节点指针headB。
两个链表相交,求两链表交点对应的节点
如果两个链表没有交点,则返回NULL;
在求交点的过程中,不可以破坏链表的结构或者修改链表的数据域;
可以确保传入的链表A与链表B没有任何环;
实现算法尽可能时间复杂度O(n),空间复杂度O(1)
#include<stdio.h>
#include<set>
using namespace std;
struct ListNode
{
int val; //数据域
ListNode* next; //指针域
ListNode(int x) : val(x), next(NULL) {} //构造函数
};
/*
// 方法一。Runtime: 76 ms, Memory Usage: 20.1 MB。时间复杂度为O(nlogn),空间复杂度为O(n)
class Solution
{
public:
Solution(){}
~Solution(){}
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB)
{
set<ListNode*> node_set;
while (headA)
{
node_set.insert(headA);
headA = headA->next;
}
while (headB)
{
if (node_set.find(headB) != node_set.end())
{
return headB;
}
headB = headB->next;
}
return NULL;
}
};
*/
//方法二。Runtime: 52 ms, Memory Usage: 16.8 MB。
int get_list_length(ListNode* head)
{
int len = 0;
while (head)
{
len++;
head = head->next;
}
return len;
}
ListNode* forward_long_list(int long_len, int short_len, ListNode* head)
{
int delta = long_len - short_len;
while (head && delta)
{
head = head->next;
delta--;
}
return head;
}
class Solution
{
public:
Solution() {}
~Solution() {}
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB)
{
int list_len_A = get_list_length(headA);
int list_len_B = get_list_length(headB);
if (list_len_A>list_len_B)
{
headA = forward_long_list(list_len_A, list_len_B, headA);
}
else
{
headB = forward_long_list(list_len_B, list_len_A, headB);
}
while (headA&&headB)
{
if (headA==headB)
{
return headA;
}
headA = headA->next;
headB = headB->next;
}
return NULL;
}
};
int main()
{
ListNode a1(1);
ListNode a2(2);
ListNode b1(3);
ListNode b2(4);
ListNode b3(5);
ListNode c1(6);
ListNode c2(7);
ListNode c3(8);
a1.next = &a2;
a2.next = &c1;
c1.next = &c2;
c2.next = &c3;
b1.next = &b2;
b2.next = &b3;
b3.next = &c1;
Solution slove;
ListNode* result = slove.getIntersectionNode(&a1, &b1);
printf("%d\n", result->val);
return 0;
}
运行结果为:
6
**
以下为Python版本
**
输入两个链表,找出它们的第一个公共节点
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
def findequal(shortPointer, longPointer, shortHead, longHead):
k = 0
while longPointer:
longPointer = longPointer.next
k += 1
shortPointer = shortHead
longPointer = longHead
for i in range(k):
longPointer = longPointer.next
while shortPointer != longPointer:
shortPointer = shortPointer.next
longPointer = longPointer.next
return shortPointer
pTmp1 = pHead1
pTmp2 = pHead2
while pTmp1 and pTmp2:
# 当两个链表的长度相同时
if pTmp1 == pTmp2:
return pTmp2
pTmp1 = pTmp1.next
pTmp2 = pTmp2.next
if pTmp1:
return findequal(pTmp2, pTmp1, pHead2, pHead1)
if pTmp2:
return findequal(pTmp1, pTmp2, pHead1, pHead2)
if __name__ == '__main__':
a1 = ListNode(1)
a2 = ListNode(2)
b1 = ListNode(3)
b2 = ListNode(4)
b3 = ListNode(5)
c1 = ListNode(6)
c2 = ListNode(7)
c3 = ListNode(8)
a1.next = a2
a2.next = c1
c1.next = c2
c2.next = c3
b1.next = b2
b2.next = b3
b3.next = c1
s = Solution()
print(s.FindFirstCommonNode(a1, b1).val)
运行结果为:
6