-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetIntersectionNode.cpp
More file actions
73 lines (59 loc) · 1.96 KB
/
GetIntersectionNode.cpp
File metadata and controls
73 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by Wanhui on 2/23/20.
//
#include "GetIntersectionNode.h"
ListNode *Solution52::getIntersectionNode(ListNode *headA, ListNode *headB) {
// 两链表为空的情况
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
unsigned int alen = getListLength(headA);
unsigned int blen = getListLength(headB);
int distance = alen - blen;
ListNode *longnode = headA;
ListNode *shortnode = headB;
// 确定长链表,并计算长链表与短链表间的距离
if (blen > alen) {
longnode = headB;
shortnode = headA;
distance = blen - alen;
}
// 长链表指针先遍历distance个节点,
// 保证两链表接下来遍历长度相等
for (int i = 0; i < distance; i++) {
longnode = longnode->next;
}
// 普通遍历,如果指针相等或遍历结束即停止
while (longnode != nullptr && shortnode != nullptr && longnode != shortnode) {
longnode = longnode->next;
shortnode = shortnode->next;
}
return longnode;
}
unsigned int Solution52::getListLength(ListNode *head) {
unsigned int length = 0;
ListNode *node = head;
// 计算链表的长度
while (node != nullptr) {
++length;
node = node->next;
}
return length;
}
ListNode *Solution52::getIntersectionNode2(ListNode *headA, ListNode *headB) {
// 两链表为空的情况
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
ListNode *pa = headA, *pb = headB;
// 如果pa与pb不相等就继续遍历
// 指针循环遍历必然出结果
// 如果两指针等长且不相交,则第一轮均遍历到pa==pb==nullptr
while (pa != pb) {
// 当pa遍历到链表A的末尾,就转向链表B继续遍历
pa = (pa == nullptr ? headB : pa->next);
// 当pb遍历到链表B的末尾,就转向链表A继续遍历
pb = (pb == nullptr ? headA : pb->next);
}
return pa;
}