
简单解:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *indexA = headA;ListNode *answer = NULL;while(indexA){ListNode *indexB = headB;while(indexB){if(indexA == indexB){answer = indexA;return answer;}indexB = indexB->next;}indexA = indexA->next;}return NULL;}
};
最优解:
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {if (!headA || !headB) {return NULL;}ListNode *you = headA, *she = headB;while (you != she) { // 若是有缘,你们早晚会相遇you = you ? you->next : headB; // 当你走到终点时,开始走她走过的路she = she ? she->next : headA; // 当她走到终点时,开始走你走过的路}// 如果你们喜欢彼此,请携手一起走完剩下的旅程(将下面这个 while 块取消注释)。// 一路上,时而你踩着她的影子,时而她踩着你的影子。渐渐地,你变成了她,她也变// 成了你。/* while (she) {you = she->next;she = you->next;} */return you;}
};
