# 链表面试题总结（一）

1.链表的冒泡排序
2.删除无头非尾节点
3.反转链表
4.在当前节点前插入一个数据x
5.查找链表的中间节点。
6.删除单链表的倒数第K个节点（K>1&&K<总长度）

# 1.链表的冒泡排序

void BubbleSort(pLinkList pList)
{

assert(pList);
{
return;
}
while (cur != tail)     //当尾指针不等于头指针时进行冒泡
{
while (cur->next != tail)   //控制cur指针最后到的位置是倒数第二个节点
{

if (cur->data > cur->next->data)
{
datatype tmp = cur->data;
cur->data = cur->next->data;
cur->next->data = tmp;
}
cur = cur->next;
}
tail = cur;
}
}


# 2.删除无头非尾节点

void EraseNotTail(pLinkNode pos)
{
assert(pos->next != NULL);      //断言，排除尾节点的情况

pos->data = pos->next->data;    //让当前节点后面节点的数据赋给当前节点。
del = pos->next;                //删除记录当前节点后的节点
pos->next = pos->next->next;    //当前节点的nex指向删除节点的下一个节点。
free(del);
del = NULL;
}


# 3.反转链表

void Reverse(pLinkList plist)
{
assert(plist);
while (cur != NULL)
{
tmp = cur;                  //
cur = cur->next;
tmp->next = newnode;
newnode = tmp;
}
}

# 4.在当前节点前插入一个数据x

void InsertFrontNode(pLinkNode pos, datatype x)
{

assert(pos);
if (NULL == newnode)
{
printf("out of memory");
exit(EXIT_FAILURE);
}
newnode->data = pos->data;
pos->data = x;
newnode->next = pos->next;
pos->next = newnode;

}


# 5.查找链表的中间节点。

pLinkNode FindMidNode(pLinkList pList)
{
assert(pList);
while (fast != NULL)
{
if (fast->next != NULL)
{
fast = fast->next->next;
}
else
{
break;;
}
slow = slow->next;
}
return slow;
}


# 6.删除单链表的倒数第K个节点（K>1&&K<总长度）

void DelKNode(pLinkList pList, int k)
{

assert(pList);
{
printf("链表为空！！\n");
return;
}
if (k == 1)
{
printf("K应当大于1\n");
return;
}
while (front != NULL)
{
k--;
if (k < 0)
{
back = back->next;
}
front = front->next;
}
if (k <= 0)
{
back->data = back->next->data;
del = back->next;
back->next = back->next->next;
free(del);
}

}