链表面试题

#pragma once
#include <stdio.h>

typedef int DateType;
typedef struct ListNode
{
    struct ListNode* next;
    DateType date;
} ListNode;
//单链表实现约瑟夫环
ListNode * JosephCycle(ListNode *first, int k)
{
    //构成环
    ListNode *tail = first;
    while (tail-> next != NULL)
    {
        tail = tail->next;
    }
    tail->next = first;

    ListNode *cur = first;
    ListNode *prve = NULL;
    while (cur->next != cur)
    {
        for (int i = 0; i < k - 1; i++){
            prve = cur;
            cur = cur->next;
        }
        prve->next = cur->next;
        free(cur);
        cur = prve->next;
    }
    cur->next = NULL;
    return cur;
}

//合并两个有序链表,合并后依然有序
ListNode * MergeOrderedList(ListNode *list1, ListNode *list2)
{
    ListNode *cur1 = list1;
    ListNode *cur2 = list2;
    ListNode *result = NULL;
    ListNode *tail = NULL;
    ListNode *next;

    while (cur1 != NULL && cur2 != NULL)
    {
        if (cur1->date <= cur2->date){
            if (result != NULL)
            {
                //结果链表不为空,尾插
                next = cur1->next;
                //插入
                tail->next = cur1;
                cur1->next = NULL;
                //保存新的最后一个节点
                tail = cur1;

                cur1 = next;
            }
            else{
                next = cur1->next;
                result = cur1;
                cur1->next = NULL;
                tail = cur1;
                cur1 = next;
            }                                  
        }
        else{
            if (result != NULL)
            {
                //结果链表不为空,尾插
                next = cur1->next;
                //插入
                tail->next = cur1;
                cur1->next = NULL;
                //保存新的最后一个节点
                tail = cur1;

                cur1 = next;
            }
            else{
                next = cur1->next;
                result = cur1;
                cur1->next = NULL;
                tail = cur1;
                cur1 = next;
            }
        }
        if (cur1 == NULL){
            tail->next = cur2;
        }
        if (cur2 == NULL){
            tail->next = cur1;
        }
        return result;
    }
}

//打印两个有序链表的交集
//小于 等于 大于
void PrintIntersection(ListNode *list1, ListNode *list2)
{
    ListNode *cur1 = list1;
    ListNode *cur2 = list2;

    while (cur1 != NULL && cur2 != NULL){
        if (cur1->date < cur2->date){
            cur1 = cur1->next;
        }
        if (cur2->date = cur2->date){
            printf("%d",cur1->date);
            cur1 = cur1->next;
            cur2 = cur2->next;
        }
        else{
            cur2 = cur2->next;
        }
    }
}

//查找单链表的中间节点,要求只能遍历一次链表
//快指针 慢指针
//慢指针走一步 快指针走两步
voidFindMid(ListNode *first)
{
    ListNode *fast = first;
    ListNode *slow = first;

    while (1){
        fast = fast->next;
        if (fast = NULL){
            break;
        }
        fast = fast->next;
        if (fast = NULL){
            break;
        }
        slow = slow->next;
    }
    printf("%d", slow->date);
}

//查找单链表的倒数第k个节点,要求只能遍历一遍
void FindTailK(ListNode *first)
{
    ListNode *forward = first;
    ListNode *backward = first;

    while (k--){
        forward = forward->next;
    }
    while (forward != NULL){
        forward = forward->next;
        backward = backward->next;
    }
    printf("%d", backward->date);
}

void ReverseList(ListNode *list)
{
    ListNode *node = NULL;
    ListNode *cur = list;
    ListNode *result = NULL;

    //循环(直到所有的结点都处理完)
    while (cur!=NULL){
        //从原链表头删的方式取一个结点
        node = cur;
        cur = cur->next;

        //将这个结点按头插的方式放入结果链表中
        node->next = result;
        result = node;
    }
    //result是结果
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值