您的位置:首页 > 职场人生

【C】单链表面试题(基础篇)

2017-06-30 15:48 176 查看
1. 比较顺序表和链表的优缺点,说说它们分别在什么场景下使用?

1)从结构上进行分析:

(1)对于顺序表,不论是静态的还是动态的,他们都是连续的存储空间,在读取上时间效率比较快,可以通过地址之间的运算来进行访问,但是在插入和删除操作会出现比较麻烦的负载操作。

(2)对于链表,因为是链式存储,在我们需要的时候才在堆上开辟空间,对于插入查找的方式比较便携。但是对于遍历的话需要多次的空间跳转。

2)从结构空间申请方式分析:

(1)顺序表的空间开辟是在满的时候进行多空间的申请开辟。往往存在着 2^n 的开辟原则。在开辟次数比较多的时候,会出现比较大的空间浪费。

(2)链表的空间开辟是针对于单个节点的空间开辟访问,不存在多余的空间浪费。并且在碎片内存池的机制下,可以有效地利用空间。

通过上面的总结,可以分析出顺序表往往用于查找遍历操作比较频繁的情况下使用。链表则针对于数据删除修改的多操作性上的情况下使用。

2. 从尾到头打印单链表

void PrintTailToHead(ListNode* pList)
{
if (pList == NULL)
return;
PrintTailToHead(pList->next);
printf("%d->", pList->data);
}


3. 删除一个无头单链表的非尾节点



void EraseNonTail(ListNode* pos)
{
assert(pos);
assert(pos->next);
ListNode* next = pos->next;
pos->data = next->data;
pos->next = next->next;
free(next);
}


4. 在无头单链表的一个节点前插入一个节点



void InsertNonHead(ListNode* pos, DataType x)
{
assert(pos);
ListNode* tmp = BuyNode(x);
ListNode* next = pos->next;
pos->next = tmp;
tmp->next = next;
DataType tmpData = pos->data;
pos->data = tmp->data;
tmp->data = tmpData;
}


5. 单链表实现约瑟夫环

       约瑟夫问题的一种描述是:编号为 1,2,…,n 的 n 个人按顺时针方向围坐一圈,每人持一个密码(正整数)。一开始任选一个正整数作为报数上限值 m,从第一个人开始按顺时针方向自 1 开始顺序报数,报到 m 时停止报数。报 m 的人出列,将他的密码作为新的 m 值,从他在顺时针方向上的下一个人开始重新从 1 报数,如此下去,直至所有人全部出列为止。



ListNode* JosephRing(ListNode* list, int k)
{
if (list == NULL)
return NULL;
ListNode* cur = list;
while (cur->next != cur)
{
int count == k;
while (--count)
{
cur = cur->next;
}
ListNode* next = cur->next;
cur->data = next->data;
cur->next = next->next;
free(next);
}
return cur;
}
//销毁
void DestoryList(ListNode** ppList)
{
ListNode* cur = *ppList;
while (cur)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
*ppList = NULL;
}


6. 逆置/反转单链表



ListNode* Reverse(ListNode* list)
{
ListNode* newList = NULL;
ListNode* cur = list;
while (cur)
{
//摘结点
ListNode* tmp = cur;
cur = cur->next;
//头插
tmp->next = newList;
newList = tmp;
}
return newList;
}


7. 单链表排序(冒泡排序 & 快速排序)



void BubbleSort(ListNode* list)
{
if (list == NULL || list->next == NULL)
return;
ListNode* tail = NULL;
while (tail != list->next)
{
int exchange = 0;
ListNode* cur = list;
ListNode* next = cur->next;
while (next != tail)
{
if ((cur->data) > next->data)
{
DataType tmp = cur->data;
cur->data = next->data;
next->data = tmp;
exchange = 1;
}
cur = cur->next;
next = next->next;
}
tail = cur;
}
}


8. 合并两个有序链表,合并后依然有序



ListNode* Merge(ListNode* list1, ListNode* list2)
{
if (list1 == NULL)
{
return list2;
}
if (list2 == NULL)
{
return list1;
}
//先摘一个结点作头
ListNode* list = NULL;
if (list2->data < list1->data)
{
list = list2;
list2 = list2->next;
}
else
{
list = list1;
list1 = list2->next;
}
ListNode* tail = list;
while (list1 && list2)
{
if (list1->data < list2->data)
{
tail->next = list1;
list1 = list1->next;
}
else
{
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
if (list1)
{
tail->next = list1;
}
else
{
tail->next = list2;
}
return list;
}


9. 查找单链表的中间节点,要求只能遍历一次链表



ListNode* FindMidNode(ListNode* list)
{
if (list == NULL)
return NULL;
ListNode* slow = list, *fast = list;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}


10. 查找单链表的倒数第 k 个节点,要求只能遍历一次链表 

ListNode* FindTailkNode(ListNode* list, int k)
{
ListNode* slow = list, *fast = list;
while (k--)
{
if (fast == NULL)
{
return NULL;
}
fast = fast->next;
}
while (fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}


11. 删除单链表的倒数第 k 个节点,要求只能遍历一次链表 

ListNode* DeleteLastNode(ListNode* head, int k)
{
if (NULL == head || k == 0)
{
return;
}
ListNode* fast = head;
while (k--)
{
if (fast == NULL)
{
return NULL;
}
fast fast->next;
}
ListNode* del = head;
ListNode* predel = NULL;
while (fast)
{
predel = del;
del = del->next;
fast = fast->next;
}
//倒数第 k 个刚好是头结点
if (del == head)
{
head = head->next;
}
else
{
predel->next = del->next;
}
delete del;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: