-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindFirstCommonNode.cpp
More file actions
101 lines (68 loc) · 1.74 KB
/
findFirstCommonNode.cpp
File metadata and controls
101 lines (68 loc) · 1.74 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
//
// Created by mi on 4/13/17.
//
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
typedef struct LNode {
int val;
LNode *next;
LNode(int x) : val(x), next(NULL) {}
} ListNode, *LinkList;
void printLinkList(LinkList linkList) {
LinkList llist = linkList;
while (llist != NULL) {
cout << "the value is :\t" << llist->val << endl;
llist = llist->next;
}
}
int getListLength(LinkList pHead) {
int len = 0;
LinkList pNode = pHead;
while (pNode != NULL) {
len++;
pNode = pNode->next;
}
return len;
}
LNode *findFirstCommonNode(LinkList pHead1, LinkList pHead2) {
int len1 = getListLength(pHead1);
int len2 = getListLength(pHead2);
int diff = len1 - len2;
LinkList headLong=pHead1;
LinkList headShort=pHead2;
if (diff < 0) {
headLong=pHead2;
headShort=pHead1;
diff=len2-len1;
}
for(int i =0; i<diff ;i++){
headLong=headLong->next;
}
while (headLong!=NULL && headShort!=NULL && headLong!=headShort){
headLong=headLong->next;
headShort=headShort->next;
}
ListNode* commonNode=headLong;
return commonNode;
}
/*
int main() {
LNode *node1 = new LNode(10);
LNode *node2 = new LNode(11);
LNode *node3 = new LNode(12);
LNode *node4 = new LNode(13);
LinkList linkList = node1;
linkList->next = node2;
linkList->next->next = node3;
LinkList linkList2 = node3;
linkList2->next = node4;
printLinkList(linkList);
cout<<findFirstCommonNode(linkList,linkList2)->val;
// cout << "===============" << endl;
// int len = getListLength(linkList);
// cout << "the len is :\t" << len << endl;
}
*/