当前位置: 首页 > news >正文

单链表经典算法

1、删除链表中指定的元素

typedef struct ListNode ListNode;

struct ListNode* removeElements(struct ListNode* head, int val) {

    // 创建一个空链表

    ListNode *newHead, *newTail;

    newHead = newTail = NULL;

    // 遍历原链表

    ListNode* pcur = head;

    while (pcur) {

        // 找值不为val的节点,尾插到新链表中

        if (pcur->val != val) {

            // 链表为空

            if (newHead == NULL) {

                newHead = newTail = pcur;

            }

            // 链表不为空

            else {

                newTail->next = pcur;

                newTail = newTail->next;

            }

           

        }

        pcur = pcur->next;

    }

    if(newTail)

    newTail->next = NULL;

    return newHead;

}




2、链表的倒置

/**

 * Definition for singly-linked list.

 * struct ListNode {

 *     int val;

 *     struct ListNode *next;

 * };

 */

 typedef struct ListNode ListNode;

struct ListNode* reverseList(struct ListNode* head) {

    if(head == NULL)

    {

        return head;

    }

    //创建三个指针

    ListNode*n1,*n2,*n3;

    n1=NULL,n2=head,n3=n2->next;

    while(n2)

    {

        n2->next = n1;

        n1 = n2;

        n2 = n3;

        if(n3)

        n3 = n3->next;

    }

    return n1;


 

}




3、找出链表中间节点

 typedef struct ListNode ListNode;

struct ListNode* middleNode(struct ListNode* head) {

    //采用快慢指针

    ListNode* slow = head;

    ListNode* fast = head;

    while(fast && fast->next)//fast要在前面,因为若fast为NULL,则对他解引用会报错,若fast为空,fast在前,就不会执行后面的

    {

        slow = slow->next;

        fast = fast->next->next;

    }

    return slow;

}




4、合并两个有序链表——带有哨兵位的链表

typedef struct ListNode ListNode;

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {

    ListNode* l1 = list1;

    ListNode* l2 = list2;

    ListNode*newhead,*newtail;

    newhead = newtail = (ListNode*)malloc(sizeof(ListNode));//此时链表不为空,头尾指针都指向了没有存储有效数据的有效地址

   

    while(l1 && l2)

    {

        if((l1->val)<=(l2->val))

        {

                newtail->next = l1;

                newtail = newtail->next;

            l1 = l1->next;

        }

        else

        {

                newtail->next = l2;

                newtail = newtail->next;

            l2 = l2->next;

        }

    }

   

       

    if(l1)

    {

        newtail->next = l1;

        newtail = newtail->next;

        l1 = l1->next;

    }

    if(l2)

    {

        newtail->next = l2;

        newtail = newtail->next;

        l2 = l2->next;

    }

    if(list1 == NULL)

    {

        return list2;

    }

    if(list2 == NULL)

    {

        return list1;

    }

    ListNode* ret = newhead->next;

    free(newhead);

    newhead = NULL;

    return ret;

}




5、环形链表的约瑟夫问题

/**

 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

 *

 *

 * @param n int整型

 * @param m int整型

 * @return int整型

 */

typedef struct ListNode ListNode;

//创建函数节点

ListNode* buynode(int x)

{

    ListNode* node = (ListNode*)malloc(sizeof(ListNode));

    if(node == NULL)

    {

        exit(1);

    }

    node->val = x;

    node->next = NULL;

    return node;

}

//创建带环链表

ListNode* createCircle(int n)

{

    //先创建第一个节点

    ListNode* phead = buynode(1);

    ListNode* ptail = phead;

    for(int i = 2;i<=n;i++)

    {

        ptail->next = buynode(i);

        ptail = ptail->next;

    }

    ptail->next = phead;

    return ptail;

}

//销毁链表

void destroy(ListNode* ps)

{

    free(ps);

    ps = NULL;

}

int ysf(int n, int m ) {

    // write code here

    //根据n创建代还链表

    ListNode* prev = createCircle(n);

    ListNode *pcur = prev->next ;

    //开始报数自杀

    int count = 1;

    while(prev->next != prev)

    {

        if(count == m)

        {

            prev->next = pcur->next;

            destroy(pcur);

            pcur = prev->next;

            count = 1;

        }

        else

        {

            //此时不需要销毁

            count++;

            prev = pcur;

            pcur = pcur->next;

        }

       

    }

    return prev->val;

}

相关文章:

  • nt!CcGetDirtyPages函数分析
  • 软件测试相关问题
  • 蓝牙无线串口入门使用教程(以大夏龙雀 WF24 和 BT36 为例)
  • PCI总线概述
  • 【开源工具】:基于PyQt5的智能网络驱动器映射工具开发全流程(附源码)
  • Java 大视界——Java大数据在智能安防视频监控中的异常事件快速响应与处理机制
  • 动态规划2——路径动态规划
  • 【消息队列】——如何实现消息保序
  • 科技文明的奇妙锻造之旅
  • 实时监控、秒级决策:镜舟科技如何重塑融资融券业务数据处理模式
  • Appium+python自动化(二十一)- Monkey指令操作手机
  • 智能云打印机EN 18031申请认证流程
  • Python—turtle绘图库使用方法
  • flutter 的lottie执行一次动画后关闭
  • 体育赛事直播平台的数据架构:从实时统计到深度洞察
  • MAC-苹果电脑专业卸载工具AppCleaner
  • 用MATLAB打造智能温度监测系统:从实时绘图到预测分析
  • 「pandas 与 numpy」数据分析与处理全流程【数据分析全栈攻略:爬虫+处理+可视化+报告】
  • 从0到1构建高并发秒杀系统:实战 RocketMQ 异步削峰与Redis预减库存
  • Spring MVC 入门案例:从代码到原理的深度剖析
  • 社交型网站首页面设计分析/2023最新15件重大新闻
  • 域名注册网站便宜/百度官网首页入口
  • 佛山营销网站建设服务/竞价网络推广外包
  • 一个公司做两个网站可以吗/广告投放网站平台
  • 可以做盗版漫画网站吗/做关键词优化
  • 网站读取错误时怎样做/南宁seo营销推广