-
Notifications
You must be signed in to change notification settings - Fork 119
/
160.c
114 lines (95 loc) · 2.39 KB
/
160.c
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
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
int lenA, lenB;
lenA = lenB = 0;
struct ListNode *ptrA, *ptrB;
ptrA = headA;
ptrB = headB;
while (ptrA != 0) {
lenA++;
ptrA = ptrA->next;
}
while (ptrB != 0) {
lenB++;
ptrB = ptrB->next;
}
int diff = lenA - lenB;
ptrA = headA;
ptrB = headB;
if (diff > 0) {
while (diff--) ptrA = ptrA->next;
}
else {
while (diff++) ptrB = ptrB->next;
}
while (ptrA != 0 && ptrB != 0) {
if (ptrA == ptrB) {
return ptrA;
}
ptrA = ptrA->next;
ptrB = ptrB->next;
}
return NULL;
}
int main() {
struct ListNode *headA
= (struct ListNode *)calloc(5, sizeof(struct ListNode));
struct ListNode *headB
= (struct ListNode *)calloc(3, sizeof(struct ListNode));
struct ListNode *headC
= (struct ListNode *)calloc(3, sizeof(struct ListNode));
struct ListNode **p = &headA;
int i;
for (i = 1; i <= 5; i++) {
(*p)->val = i;
(*p)->next = *p + 1;
p = &(*p)->next;
}
*p = NULL;
p = &headB;
for (i = 1; i <= 3; i++) {
(*p)->val = i + 5;
(*p)->next = *p + 1;
p = &(*p)->next;
}
*p = headA + 2; /* intersect A and B */
p = &headC;
for (i = 1; i <= 3; i++) {
(*p)->val = i + 5;
(*p)->next = *p + 1;
p = &(*p)->next;
}
*p = NULL;
printf("List A: ");
struct ListNode *q = headA;
while (q != NULL) {
printf("%d->", q->val);
q = q->next;
}
printf("N\n");
printf("List B: ");
q = headB;
while (q) {
printf("%d->", q->val);
q = q->next;
}
printf("N\n");
printf("List C: ");
q = headC;
while (q) {
printf("%d->", q->val);
q = q->next;
}
printf("N\n");
struct ListNode *ret;
ret = getIntersectionNode(headA, headB);
printf("Ptr of A B: %p, val: %d\n", ret, (ret != NULL) ? ret->val : -1);
ret = getIntersectionNode(headC, headB);
printf("Ptr of C B: %p, val: %d\n", ret, (ret != NULL) ? ret->val : -1);
return 0;
}