-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoLinkedLists.cpp
More file actions
179 lines (174 loc) · 4.41 KB
/
IntersectionOfTwoLinkedLists.cpp
File metadata and controls
179 lines (174 loc) · 4.41 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
God Bless Me BUG Free Forever
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/***
* 法1:
* 1 分别求出两个链表的长度lenA lenB
* 2 短的先走|lenA - lenB|
* 3 然后两个链表一起往后走,直到相遇或者某条链走完
***/
/*
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return NULL;
int lenA = getListLength(headA);
int lenB = getListLength(headB);
ListNode *node1 = headA;
ListNode *node2 = headB;
if (lenA > lenB)
{
int dif = lenA - lenB;
while (dif--)
node1 = node1->next;
}
else if (lenA < lenB)
{
int dif = lenB - lenA;
while (dif--)
node2 = node2->next;
}
while (node1 && node2)
{
if (node1 == node2)
return node1;
node1 = node1->next;
node2 = node2->next;
}
return NULL;
}
private:
int getListLength(ListNode *head)
{
ListNode *node = head;
int len = 0;
while (node)
{
++len;
node = node->next;
}
return len;
}
};
*/
/***
* 法2:
* 1 将链表B连接到A的后面
* 2 在链表A中找环,若有环,则环的入口则为两链表交点
* 3 断开两链表
***/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return NULL;
int lenA = connectList(headA, headB);
ListNode *node = findCycleEnt(headA);
breakList(headA, lenA); // ERROR: linked structure was modified.
return node;
}
private:
int connectList(ListNode *headA, ListNode *headB)
{
if (!headA || !headB) return 0;
ListNode *node = headA;
int lenA = 1;
while (node->next)
{
++lenA;
node = node->next;
}
node->next = headB;
return lenA;
}
ListNode *findCycleEnt(ListNode *head)
{
if (!head) return NULL;
ListNode *slow = head;
ListNode *fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (slow != fast) return NULL;
slow = head;
while (slow != fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
void breakList(ListNode *head, int len)
{
if (!head) return;
ListNode *node = head;
while (--len)
node = node->next;
node->next = NULL;
return;
}
};
/***
* 法3:
* 两个单链表相交成 Y 字型,两个指针分别从链表头往后遍历
* 遍历到尾,则从另一个链表的头再开始遍历
* 若链表相交,则两指针刚好相遇在交点处(都走完了Y字的三条边)
***/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return NULL;
ListNode *node1 = headA;
ListNode *node2 = headB;
bool twice1 = false, twice2 = false;
while ((node1 && node2) || (!node1 && twice1) || (node2 && twice2))
{
if (twice1 && twice2 && (node1 == node2))
return node1;
if (!node1->next && !twice1)
{
twice1 = true;
node1 = headB;
}
else
node1 = node1->next;
if (!node2->next && !twice2)
{
twice2 = true;
node2 = headA;
}
else
node2 = node2->next;
}
return NULL;
}
};