# 链表的常见面试题

##### 逆序打印单链表
{
PNode pTail = NULL;
PNode pPre = NULL;
return;
while (pCur)
{
pTail = pCur;
pCur = pCur->_pNext;
}
{
while (pCur!=pTail)
{
pPre = pCur;
pCur = pCur->_pNext;

}
printf("%d->", pTail->_data);
pTail = pPre;
}
printf("%d->NULL\n", pTail->_data);

//递归实现
#if 0
{
}

#endif
}

##### 删除链表的非尾结点，要求：不能遍历链表
void DeleteListNotTailNode(PNode pos)
{
PNode pNext = NULL;
pNext = pos->_pNext;
pos->_data = pNext->_data;
pos->_pNext = pNext->_pNext;
free(pNext);
pNext = NULL;
}

##### 在链表pos位置前插入值到data的结点
/*在链表后边插入，然后将pos的值给后边的，再将值给pos*/
void InesrtPosFront(PNode pos, DataType data)
{
pNext->_pNext = pos->_pNext;
pos->_pNext = pNext;
pNext->_data = pos->_data;
pos->_data = data;
}

##### 查找链表的中间结点，要求只能遍历一次链表
/*定义两个指针，一个一次走两步，一个走一步*/
{
{
return NULL;
}
{
}
else
{
while (pFast&&pFast->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;
}
return  pSlow;
}
}

##### 查找链表的倒数第K个结点，要求只能遍历一次链表
{
if (NULL == pHead || K <= 0)
{
return NULL;
}
while (K--)
{
pFast = pFast->_pNext;
}
while (pFast)
{
pFast = pFast->_pNext;
pSlow = pSlow->_pNext;
}

return pSlow;
}

// 删除链表的倒数第K个结点，要求只能遍历一次链表

{
PNode pPre = NULL;
if (NULL == pHead || K <= 0)
{
return;
}
while (K--)
{
pFast = pFast->_pNext;
}
while (pFast)
{
pFast = pFast->_pNext;
pPre = pSlow;
pSlow = pSlow->_pNext;
}
pPre->_pNext = pSlow->_pNext;
free(pSlow);
pSlow = NULL;

}

##### 用单链表实现约瑟夫环
void JosephCircle(PNode* pHead, const int M)
{
PNode pCur = NULL;
PNode pDel = NULL;
int count = 0 ;
if (NULL == *pHead|| M <= 0)
{
return;
}
//先遍历链表，让尾指向头
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
while ((pCur)->_pNext != pCur)
{
count = M;
while (--count)                     //为什么时--M
{
pCur = pCur->_pNext;
}
//删节点
pDel = pCur->_pNext;
pCur->_data = pDel->_data;
pCur->_pNext = pDel->_pNext;
free(pDel);
pDel = NULL;

}
//解环
pCur->_pNext = NULL;

SListPrint(pCur);
}

##### 链表的逆置–三个指针

{
PNode pPre = NULL;
PNode pCur = NULL;
PNode pNext = NULL;
while (pCur)
{
pNext = pCur->_pNext;
pCur->_pNext = pPre;
pPre = pCur;
pCur = pNext;
}
}

##### 链表的逆置–头插法

{
PNode pCur = NULL;
PNode pNewNode= NULL;
return NULL;

{
pCur->_pNext = pNewNode;
pNewNode = pCur;
}

}

##### 用冒泡排序思想对单链表进行排序

{
PNode pTail = NULL;
int flag = 0;
{
return;
}
{
flag = 0;
//这里为pCur->_pNext是为了防止if语句的中pCur->_pNext为空，如果为空第一次比较程序就会崩溃
while (pCur->_pNext != pTail)
{
if (pCur->_data < pCur->_pNext->_data)
{
DataType temp = pCur->_data;
pCur->_data = pCur->_pNext->_data;
pCur->_pNext->_data = temp;
flag = 1;
}

pCur = pCur->_pNext;
}
pTail = pCur;
if (0 == flag)
break;
}
}

##### 合并两个有序单链表，合并后依然有序
{
PNode pCur = NULL;

{
}
{
}

{
{
pCur = pCur->_pNext;
}
{
pCur = pCur->_pNext;
}
}
{
pCur = pCur->_pNext;
}
{
pCur = pCur->_pNext;
}

}

##### 判断两个单链表是否相交—链表不带环
{
return 0;
//找到链表的最后一个节点
{
}
{
}
{
return 1;
}
else
{
return 0;
}
}

##### 求两个单链表相交的交点—链表不带环
/*先让长的链表遍历size1-size2，再让两个链表一起遍历，
当pCur1==pCur2时，两链表相交*/
{
return NULL;
int size1 = 0;
int size2 = 0;
int gap = 0;
while (pCur1)
{
pCur1 = pCur1->_pNext;
size1++;
}
while (pCur2)
{
pCur2 = pCur2->_pNext;
size2++;
}
gap = size1 - size2;
if (gap > 0)
{
while (gap--)
{
pCur1 = pCur1->_pNext;
}
}
else
{
while (gap++)
{
pCur2 = pCur2->_pNext;
}
}
while (pCur1 != pCur2)
{
pCur1 = pCur1->_pNext;
pCur2 = pCur2->_pNext;
}
return pCur2;
}

##### 测试程序
void TestInterview()
{

//SListPrint(pCur);

//DeleteListNotTailNode(pCur);
//InesrtPosFront(pCur, 5);

}

##### 判断两个链表是否相交，链表可能带环
{
return NULL;

while (pFast && pFast->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;

if (pSlow == pFast)
return pSlow;
}

return NULL;
}

##### 获取环的入口点
{
PNode pM = pMeetNode;
if (NULL == pHead1 || NULL == pMeetNode)
{
return NULL;
}
else
{
while (pH != pM)
{
pH = pH->_pNext;
pM = pM->_pNext;
}
}

return pM;
}

##### 获取环的长度
int GetCircleLen(PNode pMeetNode)
{
PNode pCur = pMeetNode;
int len = 1;
if (NULL == pMeetNode)
{
return 0;
}
else
{
while (pCur->_pNext != pMeetNode)
{
pCur = pCur->_pNext;
len++;
}
}

return len;
}

##### 判断两个链表是否相交，链表可能带环

{
PNode pSlow = NULL;
PNode pFast = NULL;
return 0;

while (pFast&&pFast->_pNext)
{
pFast = pFast->_pNext->_pNext;
pSlow = pSlow->_pNext;
if (pFast == pSlow)
return 1;
}

return 0;
}

##### 测试
void TestInterview2()
{

while (NULL != pCur->_pNext)
pCur = pCur->_pNext;

int len = GetCircleLen(pMeetNode);

}

##### 复杂链表的复制
{
PCSLNode pNewNode = NULL;
return NULL;

//1.在链表后面插入值相同的新节点
while (pCur)
{
pNewNode->_pNext = pCur->_pNext;
pCur->_pNext = pNewNode;
pCur = pNewNode->_pNext;
}

//2.给新节点的随机指针域赋值
//让随机指针域指向原节点随机指针指向的下个节点
while (pCur)
{
pNewNode = pCur->_pNext;
if (pCur->_pRandom)
{
pNewNode->_pRandom = pCur->_pRandom->_pNext;
}
pCur = pNewNode->_pNext;

}

//3.将新节点从原链表中拆下来
while (pCur->_pNext)
{
pNewNode = pCur->_pNext;
pCur->_pNext = pNewNode->_pNext;
pCur = pNewNode;

}

}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120