# 剑指offer（三）链表

## 面试题5：从尾到头打印链表

    vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
stack<ListNode*> nodes;
while(pNode != NULL){
nodes.push(pNode);
pNode = pNode->next;
}
while(!nodes.empty()){
pNode = nodes.top();
res.push_back(pNode->val);
nodes.pop();
}
return res;
}

## 面试题13：O(1)时间删除链表结点

void DeleteNode(ListNode** phead,ListNode* pToBeDelete)
{
if (*phead == NULL || pToBeDelete == NULL)
return;

//删除非尾节点
if (pToBeDelete->_next != NULL)
{
ListNode* pNext = pToBeDelete->_next;
pToBeDelete->_data = pNext->_data;
pToBeDelete->_next = pNext->_next;

delete pNext;
pNext = NULL;
}

//链表只有一个节点，删除头结点（也是尾节点）
{
delete pToBeDelete;
pToBeDelete = NULL;
}

//链表有多个节点，删除节点是尾节点
else
{
while (cur->_next != pToBeDelete)
{
cur = cur->_next;
}
delete pToBeDelete;
pToBeDelete = nullptr;
cur->_next = nullptr;
}
}  

## 面试题15：链表中倒数第k个结点

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead == NULL || k == 0)
return NULL;
for(int i=0; i<k-1 ;i++){
else
return NULL;
}
pBefore = pBefore->next;
}
return pBefore;
}
};

## 面试题16：反转链表

class Solution {
public:
ListNode *pPrev = NULL;
while(pNode != NULL){
ListNode *pNext = pNode->next;
if(pNext == NULL)
pNode->next = pPrev;
pPrev = pNode;
pNode = pNext;
}
}
};

## 面试题17：合并两个排序的链表

class Solution {
public:
{
}else{
}
}
};

## 面试题26：

struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};

class Solution {
public:
{
}
while(pNode != NULL){
RandomListNode* pCloned = new RandomListNode(pNode->label);
pCloned->next = pNode->next;
pNode->next = pCloned;
pNode = pCloned->next;
}
}
while(pNode != NULL){
RandomListNode* pCloned = pNode->next;
if(pNode->random != NULL)
pCloned->random = pNode->random->next;
pNode = pCloned->next;
}
}
RandomListNode* pClonedNode = NULL;
if(pNode != NULL){
pNode->next = pClonedNode->next;
pNode = pNode->next;
}
while(pNode != NULL){
pClonedNode->next = pNode->next;
pClonedNode = pClonedNode->next;
pNode->next = pClonedNode->next;
pNode = pNode->next;
}
}
};

## 面试题27：二叉搜索树与双向链表

class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
TreeNode *pLastNodeInList = NULL;
ConvertNode(pRootOfTree,&pLastNodeInList); //pLastNodeInList指向双向链表的尾结点
}
}
void ConvertNode(TreeNode* pNode, TreeNode** pLastNodeInList){
if(pNode == NULL)
return;
TreeNode * pCurrent = pNode;
if(pCurrent->left != NULL)
ConvertNode(pCurrent->left,pLastNodeInList);
pCurrent->left = *pLastNodeInList;
if(*pLastNodeInList != NULL)
(*pLastNodeInList)->right = pCurrent;
*pLastNodeInList = pCurrent;
if(pCurrent->right != NULL)
ConvertNode(pCurrent->right,pLastNodeInList);
}
};

## 面试题56：链表中环的入口结点

class Solution {
public:
return NULL;
if(pSlow == NULL)
return NULL;
ListNode* pFast = pSlow->next;
while(pSlow!=NULL && pFast!=NULL){
if(pSlow == pFast)
return pFast;
pSlow = pSlow->next;
pFast = pFast->next;
if(pFast != NULL)
pFast = pFast->next;
}
return NULL;
}
{
//判断链表是否是环形，若是环形返回环形中的某个结点meetingNode
if(meetingNode == NULL)
return NULL;
//求出环中结点数目m
ListNode* pNode1 = meetingNode;
int nodesInLoop = 1;
while(pNode1->next != meetingNode){
nodesInLoop++;
pNode1 = pNode1->next;
}
//两个指针，一个指针先走m步后，另一个指针再出发，两个相遇点即是环的入口结点
for(int i=0;i<nodesInLoop;i++)
pNode1 = pNode1->next;
while(pNode1 != pNode2){
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
return pNode1;
}
};

## 面试题57：删除链表中重复的结点

class Solution {
public:
{
return NULL;
ListNode* pPreNode = NULL; //保存需要删除的结点的前一个结点
while(pNode != NULL){
ListNode* pNext = pNode->next;
bool needDelete = false;
if(pNext!=NULL && pNext->val==pNode->val)//判断是否有重复的数
needDelete = true;
if(!needDelete){ //没有重复的数，就往前推进
pPreNode = pNode;
pNode = pNode->next;
}else{ //有重复的数，删除它们
int value = pNode->val;
ListNode* pToBeDel = pNode;
while(pToBeDel != NULL && pToBeDel->val == value){
pNext = pToBeDel->next;
delete pToBeDel;
pToBeDel = pNext;
}
if(pPreNode == NULL)
else
pPreNode->next = pNext;
pNode = pNext;
}
}
}
};

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

120