链表的面试题(二)

完成链表的以下操作:

// 使用冒泡对单链表进行排序
void BubbleSort(PNode pHead)

// 合并两个已序链表,合并之后新链表依然有序
PNode MergeList(PNode pHead1, PNode pHead2)

// 查找无头单链表的倒数第K个结点
PNode FindLastKNode(PNode pHead, int K);

// 删除无头单链表的倒数第K个结点
PNode DeleteLastKNode(PNode pHead, int K);

// 判断链表是否带环,如果带环求环的长度,并给出入口点
PNode HasCircle(PNode pHead);
int GetCircleLen(PNode pHead);
PNode GetEnterNode(PNode pHead, PNode pMeetNode);

源程序

void BubbleSort(PNode pHead)//冒泡排序
{
    if (NULL == pHead || NULL == pHead->next)
    {
        return;
    }
    PNode pCur = pHead;
    PNode pTail = NULL;
    int flag = 0;
    while (pTail != pHead)
    {
        pCur = pHead;
        while (pCur->next != pTail)
        {
            if (pCur->data > pCur->next->data)
            {
                DataType tmp = pCur->data;
                pCur->data = pCur->next->data;
                pCur->next->data = tmp;
                flag = 1;
            }
            pCur = pCur->next;
            if (!flag)
            {
                return;
            }
        }
        pTail = pCur;
    }
}

这里写图片描述

PNode FindLastKNode(PNode pHead, int K)
{
    if (NULL == pHead || K == 0)
    {
        return NULL;
    }
    PNode pSlow = pHead;
    PNode pFast = pHead;
    while (--K)
    {
        if (NULL == pFast)
        {
            return NULL;
        }
        pFast = pFast->next;
    }
    while (pFast->next)
    {
        pSlow = pSlow->next;
        pFast = pFast->next;
    }
    return pSlow;
}
void DeleteLastKNode(PNode pHead, int  K)
{
    if (NULL == pHead || K == 0)
    {
        return;
    }
    PNode pos = FindLastKNode(pHead, K);
    Erase(pHead, pos);

PNode MergeList(PNode pHead1, PNode pHead2);
考虑以下因素:
pHead1为空—–返回pHead2
pHead2为空—–返回pHead1
当pHead1和pHead2都不为空时,分析如图示:
这里写图片描述

PNode MergeList(PNode pHead1, PNode pHead2)
{
    if (NULL == pHead1)
    {
        return pHead2;
    }
    if (NULL == pHead2)
    {
        return pHead1;
    }
    PNode newNode = NULL;
    PNode pTail = NULL;
    if (pHead1->data < pHead2->data)
    {
        pTail = pHead1;
        pHead1 = pHead1->next;
    }
    else
    {
        pTail = pHead2;
        pHead2 = pHead2->next;
    }
    newNode = pTail;
    while (pHead1 && pHead2)
    {
        if (pHead1->data < pHead2->data)
        {
            pTail->next = pHead1;
            pTail = pHead1;
            pHead1 = pHead1->next;
        }
        else
        {
            pTail->next = pHead2;
            pTail = pHead2;
            pHead2 = pHead2->next;
        }
    }
    if (pHead1)
    {
        pTail->next = pHead1;
    }
    else
    {
        pTail->next = pHead2;
    }
    return newNode;
}
// 判断链表是否带环,若带环给出相遇点  
PNode HasCircle(PNode pHead)

{
    if (pHead == NULL)
    {
        return NULL;
    }
    else
    {

        PNode pFast = pHead;
        PNode pSlow = pHead;
        {
            while (pFast && pFast->next)
            {
                pSlow = pSlow->next;
                pFast = pFast->next->next;
                if (pFast == pSlow)
                {
                    return pSlow;
                }
            }
            return NULL;
        }
    }
}

// 求环的长度  
int   GetCircleLen(PNode pMeetNode)
{
    PNode pCurNode = pMeetNode;
    int count = 1;
    while (pCurNode->next != pMeetNode)
    {
        pCurNode = pCurNode->next;
        count++;
    }
    return count;
}

// 求环的入口点  
PNode GetEnterNode(PNode pHead, PNode pMeetNode)

{
    if (pHead == NULL)
    {
        return NULL;
    }
    else
    {
        PNode pFast = pHead;
        PNode pSlow = pMeetNode;
        {
            while (pFast != pSlow)
            {
                pSlow = pSlow->next;
                pFast = pFast->next;
            }
            return pSlow;
        }
    }
}
<think>我们正在处理一个关于C语言链表面试题的问题。根据用户的问题,我们需要提供一些常见的C语言链表面试题,并给出解答。参考引用内容,我们可以总结出一些常见的链表面试题:1.从尾到头打印单链表(递归和非递归)2.删除一个无头单链表的非尾节点(不能遍历链表)3.移除链表元素4.反转链表5.链表的中间节点6.返回倒数第k个节点7.合并两个有序链表8.分割链表9.回文链表10.相交链表11.环形链表(判断是否有环,返回入环的第一个节点)12.随机链表的复制(深度拷贝)我们选择其中几个经典题目进行详细解答。注意:在回答中,我们需要使用正确的LaTeX格式,并且按照要求使用$...$和$$...$$。同时,回答使用中文,并在最后生成相关问题。由于引用中已经提供了一些代码和思路,我们可以参考这些内容,但需要用自己的话组织,并确保正确性。下面开始组织回答。</think>### C语言链表常见面试题解析 链表是数据结构中的核心内容,也是C语言面试的必考点。以下是常见题型及解析: #### 1. 反转链表 **题目要求**:将单链表所有节点逆序排列。 ```c struct ListNode* reverseList(struct ListNode* head) { struct ListNode *prev = NULL, *cur = head; while (cur) { struct ListNode *next = cur->next; // 保存后继节点 cur->next = prev; // 反转指针 prev = cur; // 前移prev cur = next; // 前移cur } return prev; // 新头节点 } ``` **核心思想**:三指针法(prev/cur/next),时间复杂度 $O(n)$,空间复杂度 $O(1)$[^2]。 #### 2. 环形链表检测 **题目要求**:判断链表是否有环,并返回入环节点。 ```c struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) { // 相遇点 while (head != slow) { // 找入环节点 head = head->next; slow = slow->next; } return slow; } } return NULL; } ``` **数学原理**:设头到入环点距离 $a$,环长 $L$,快慢指针速度比 $2:1$,相遇时慢指针位移 $s$,则 $2s = s + nL$ → $s = nL$。重置指针后同步移动 $a$ 步必在入环点相遇[^3]。 #### 3. 删除无头链表非尾节点 **题目要求**:在无头节点指针情况下删除指定节点(非尾节点)。 ```c void deleteNode(struct ListNode* node) { node->val = node->next->val; // 复制后继值 struct ListNode* tmp = node->next; node->next = tmp->next; // 跳过后继节点 free(tmp); // 释放内存 } ``` **关键点**:通过值复制和指针跳过实现 $O(1)$ 删除[^1]。 #### 4. 合并两个有序链表 **题目要求**:将两个升序链表合并为一个新链表。 ```c struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { if (!l1) return l2; if (!l2) return l1; if (l1->val < l2->val) { l1->next = mergeTwoLists(l1->next, l2); return l1; } else { l2->next = mergeTwoLists(l1, l2->next); return l2; } } ``` **优化方向**:递归法空间复杂度 $O(n)$,可改为迭代法优化至 $O(1)$[^4]。 #### 5. 复杂链表的复制 **题目要求**:深度拷贝带随机指针的链表。 ```c struct Node* copyRandomList(struct Node* head) { if (!head) return NULL; // 1. 创建交织链表: A->A'->B->B' struct Node* cur = head; while (cur) { struct Node* copy = (struct Node*)malloc(sizeof(struct Node)); copy->val = cur->val; copy->next = cur->next; cur->next = copy; cur = copy->next; } // 2. 复制随机指针 cur = head; while (cur) { if (cur->random) cur->next->random = cur->random->next; cur = cur->next->next; } // 3. 分离链表 cur = head; struct Node* newHead = head->next; while (cur) { struct Node* tmp = cur->next; cur->next = tmp->next; if (tmp->next) tmp->next = tmp->next->next; cur = cur->next; } return newHead; } ``` **核心技巧**:三次遍历(插入/复制随机指针/分离),时间复杂度 $O(n)$[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值