-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37_FindFirstCommonNodeInLists.cpp
More file actions
117 lines (93 loc) · 2.47 KB
/
37_FindFirstCommonNodeInLists.cpp
File metadata and controls
117 lines (93 loc) · 2.47 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
# include <iostream>
# include <cstdlib>
# include <vector>
# include <unordered_set>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) // struct的构造函数
: val(x), next(NULL)
{}
};
void display_list(ListNode* head)
{
if (head == NULL)
return;
for(ListNode* p = head; p != NULL; p = p->next)
cout << p->val << ' ';
cout << endl;
}
ListNode* FindFirstCommonNode_Set(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 == NULL || pHead2 == NULL)
return NULL;
unordered_set<ListNode *> first;
ListNode *p;
// set of first list nodes
for (p = pHead1; p != NULL; p = p->next)
first.insert(p);
// check every node in second list
for (p = pHead2; p != NULL; p = p->next)
if (first.find(p) != first.end())
return p;
return NULL;
}
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 == NULL || pHead2 == NULL)
return NULL;
int len_long = 0, len_short = 0, i = 0;
ListNode *p, *q, *head_long = pHead1, *head_short = pHead2;
// count length
for (p = pHead1; p != NULL; p = p->next)
len_long += 1;
for (p = pHead2; p != NULL; p = p->next)
len_short += 1;
if (len_short > len_long)
{
swap(head_long, head_short);
swap(len_long, len_short);
}
// walk long list
for (i = 0, p = head_long; i < len_long - len_short; i++)
p = p->next;
// walk simultaneously until first common
for (q = head_short; p != NULL && q != NULL; p = p->next, q = q->next)
if (p == q)
return p;
return NULL;
}
int main(int argc, char *argv[])
{
int arr[] = {1, 2, 3, 6, 7};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
ListNode *node = NULL, *head1 = NULL, *tail = NULL, *res = NULL, *head2;
// list 1
for (int i = 0; i < vec.size(); i++)
{
node = new ListNode(vec[i]);
if (head1 == NULL)
head1 = tail = node;
else
{
tail->next = node;
tail = node;
node = NULL;
}
}
display_list(head1);
// list 2
head2 = node = new ListNode(4);
node = new ListNode(5);
head2->next = node;
node->next = head1->next->next->next;
display_list(head2);
res = FindFirstCommonNode(head1, head2);
if (res != NULL)
cout << res->val << endl;
else
cout << "Error." << endl;
return 0;
}