-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection_of_two_linked_lists.c
More file actions
57 lines (54 loc) · 1.21 KB
/
Copy pathintersection_of_two_linked_lists.c
File metadata and controls
57 lines (54 loc) · 1.21 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode* p1 = headA;
struct ListNode* p2 = headB;
while(p1 != p2){
p1 = p1 == NULL ? headB : p1->next;
p2 = p2 == NULL ? headA : p2->next;
}
return p1;
// Get len of both lists
//int a_len = 0;
//int b_len = 0;
//
//struct ListNode* a = headA;
//struct ListNode* b = headB;
//while(a) {
// a_len++;
// a = a->next;
//}
//while(b) {
// b_len++;
// b = b->next;
//}
//
//// move the longer list ahead by diff
//a = headA;
//b = headB;
//if(a_len > b_len) {
// int len_diff = a_len - b_len;
// for(int i = 0; i < len_diff; ++i) {
// a = a->next;
// }
//}
//else {
// int len_diff = b_len - a_len;
// for(int i = 0; i < len_diff; ++i) {
// b = b->next;
// }
//}
//
//// go until we find the common pointer
//while(a != b) {
// a = a->next;
// b = b->next;
//}
//
//return a;
}