-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffer-52-FirstCommonNodesInLists.c
More file actions
105 lines (92 loc) · 1.63 KB
/
offer-52-FirstCommonNodesInLists.c
File metadata and controls
105 lines (92 loc) · 1.63 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
#include <stdio.h>
#include "minunit.h"
// https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
/**
* Definition for singly-linked list.
*/
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
if (headA == NULL || headB == NULL)
{
return NULL;
}
struct ListNode *nodeA = headA;
struct ListNode *nodeB = headB;
while (nodeA != nodeB)
{
if (nodeA != NULL)
{
nodeA = nodeA->next;
}
else
{
nodeA = headB;
}
if (nodeB != NULL)
{
nodeB = nodeB->next;
}
else
{
nodeB = headA;
}
}
return nodeA;
}
struct ListNode *getIntersectionNode1(struct ListNode *headA, struct ListNode *headB)
{
struct ListNode *headToA = headA, *headToB = headB;
int countA = 0, countB = 0;
while (headToA != NULL)
{
headToA = headToA->next;
countA++;
}
while (headToB != NULL)
{
headToB = headToB->next;
countB++;
}
headToA = headA, headToB = headB;
if (countA > countB)
{
int gap = countA - countB;
for (int i = 0; i < gap; i++)
{
headToA = headToA->next;
}
}
else if (countB > countA)
{
int gap = countB - countA;
for (int i = 0; i < gap; i++)
{
headToB = headToB->next;
}
}
while (headToA != headToB)
{
headToA = headToA->next;
headToB = headToB->next;
}
return headToA;
}
MU_TEST(test_case)
{
mu_check(5 == 7);
}
MU_TEST_SUITE(test_suite)
{
MU_RUN_TEST(test_case);
}
int main()
{
MU_RUN_SUITE(test_suite);
MU_REPORT();
return MU_EXIT_CODE;
}