51,394
社区成员




#define N 5
int hash(int element){
return element*2654435761;
}
int shardingIndex(int a){
int p = hash(a);
_________________________; //这里是空格
return p;
}
struct node
{
int v;
node *next;
};
/*
返回链表的长度
链表为空 返回0
*/
size_t listLen(node * p)
{
size_t num = 0;
while (p!=NULL)
{
num++;
p = p->next;
}
return num;
}
// 如果找到了 则返回指针 指向公共节点
// 如果不存在 则返回空指针
node * findFirstCommenNode(node * pheada, node * pheadb)
{
size_t lenA = listLen(pheada);
size_t lenB = listLen(pheadb);
node * plistA = pheada;
node * plistB = pheadb;
//调整长度
//plistA 指向较长的一个
if (lenA < lenB)
{
plistB = pheada;
plistA = pheadb;
size_t t = lenA;
lenA = lenB;
lenB = t;
}
while(lenA > lenB)
{
plistA = plistA->next;
--lenA;
}
//一样长了
//寻找公共节点
while (plistA!=NULL && plistA != plistB)
{
plistA = plistA->next;
plistB = plistB->next;
}
return plistA;
}
算法的空间复杂度O(1),时间复杂度O(m+n),效果不错吧。