链表的应用

⽬录

1. 单链表经典算法OJ题⽬

1.1 单链表相关经典算法OJ题1:移除链表元素

203. 移除链表元素 - 力扣(LeetCode)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode LN;
struct ListNode* removeElements(struct ListNode* head, int val) {
    LN* newhead,* newtail;
    newhead=newtail=NULL;

    while(pcur)
    {
        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;
}

1.2 单链表相关经典算法OJ题2:206. 反转链表 - 力扣(LeetCode)

思路1.创建一个新i链表然后头插

思路2.创建三个指针,分别定义前驱节点,当前节点,后续节点,来反转链表具体实现如下,

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode LN;
struct ListNode* reverseList(struct ListNode* head) {
    if(head==NULL)
    {
        return head;
    }
    LN* n1,*n2,*n3;
    n1=NULL;
    n2=head;
    n3=head->next;
    LN* pcur=head;

    while(n2)
    {
        n2->next=n1;
        n1=n2;
        n2=n3;
        if(n3)
        {
            n3=n3->next;    
        }
    }
    return n1;
}

1.3 单链表相关经典算法OJ题3:21. 合并两个有序链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode LN;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if(list1==NULL)
    {
        return list2;
    }
    else if(list2==NULL)
    {
        return list1;
    }
    else if(list1==NULL && list2==NULL) 
    {
        return NULL;

    }

    LN* l1=list1;
    LN* l2=list2;
    LN* newNode,* newTail;
    newNode=newTail=NULL;

    while(l1 && l2)
    {
        if(l1->val<l2->val)
        {
            if(newNode==NULL)
            {
                newNode=newTail=l1;
            }
            else
            {
                newTail->next=l1;
                newTail=newTail->next;
            }
            l1=l1->next;
        }
        else
        {
            if(newNode==NULL)
            {
                newNode=newTail=l2;
            }
            else
            {
                newTail->next=l2;
                newTail=newTail->next;
            }
            l2=l2->next;
        }
    }
    if(l1)
    {
        newTail->next=l1;
    }
    if(l2)
    {
        newTail->next=l2;
    }
    return newNode;
}

通过上面代码可以发现有大部分重复的代码,在原来的基础上还是可以优化的

通过上面可以发现这些代码很多重复的都在判断节点是不是空的所以我们这里采用带头的单链表这样就可以省去判断的时间

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode LN;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if(list1==NULL)
    {
        return list2;
    }
    else if(list2==NULL)
    {
        return list1;
    }
    else if(list1==NULL && list2==NULL) 
    {
        return NULL;

    }

    LN* l1=list1;
    LN* l2=list2;
    LN* newNode,* newTail;
    newNode=newTail=(LN*)malloc(sizeof(LN));

    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;
    }
    if(l2)
    {
        newTail->next=l2;
    }
    return newNode->next;
}

1.4 单链表相关经典算法OJ题4876. 链表的中间结点 - 力扣(LeetCode)

快慢指针:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode LN;
struct ListNode* middleNode(struct ListNode* head) {
    LN* fast,* slow;
    fast=slow=head;
    while(fast && fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    return slow;

}

1.5 循环链表经典应⽤-环形链表的约瑟夫问题_牛客题霸_牛客网 (nowcoder.com)

据说著名犹太 历史学家 Josephus有过以下的故事:在罗⻢⼈占领乔塔帕特后,39 个犹太⼈与
Josephus及他的朋友躲到⼀个洞中,39个犹太⼈决定宁愿死也不要被⼈抓到,于是决定了⼀个⾃杀 ⽅式,41个⼈排成⼀个圆圈,由第1个⼈开始报数,每报数到第3⼈该⼈就必须⾃杀,然后再由⼀ 个重新报数,直到所有⼈都⾃杀⾝亡为⽌。
然⽽Josephus 和他的朋友并不想遵从,Josephus要他的朋友先假装遵从,他将朋友与⾃⼰安排在
第16个与第31个位置,于是逃过了这场死亡游戏。
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @param m int整型 
 * @return int整型
 */
 #include <stdlib.h>
typedef struct ListNode LN ;
LN* BuyNode(int x)
{
    LN* newnode=(LN*)malloc(sizeof(LN));
    newnode->val=x;
    newnode->next=NULL;
    return newnode;
}
LN* create(int n)
{
    LN* phead=BuyNode(1);
    LN* ptail=phead;
    for (int i=2; i<=n; i++) 
    {
        ptail->next=BuyNode(i);
        ptail=ptail->next;
    }
    ptail->next=phead;
    return ptail;
}
int ysf(int n, int m ) {
    LN* prev=create(n);
    LN* pcur=prev->next;
    int count=1;
    while(pcur->next!=pcur)
    {
        if(count==m)
        {
            prev->next=pcur->next;
            pcur=pcur->next;
            count=1;
        }
        else 
        {
            prev=pcur;
            pcur=pcur->next;
            count++;
        }
    }
    return pcur->val;
}

1.6 单链表相关经典算法OJ题5:分割链表

面试题 02.04. 分割链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode LN;
struct ListNode* partition(struct ListNode* head, int x){
    if(head==NULL)
    {
        return head;
    }
    LN* bigHead=(LN*)malloc(sizeof(LN));
    LN* bigTail=bigHead;

    LN* smallHead=(LN*)malloc(sizeof(LN));
    LN* smallTail=smallHead;

    LN* pcur=head;
    while(pcur)
    {
        if(pcur->val<x)
        {
            smallTail->next=pcur;
            smallTail=smallTail->next;
        }
        else
        {   
            bigTail->next=pcur;
            bigTail=bigTail->next;
            
        }     
        pcur=pcur->next;
    }
    bigTail->next=NULL;
    smallTail->next=bigHead->next;
    
    return smallHead->next;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/517530.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于opencv的猫脸识别模型

opencv介绍 OpenCV的全称是Open Source Computer Vision Library&#xff0c;是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发&#xff0c;以BSD许可证授权发行&#xff0c;可以在商业和研究领域中免费使用。OpenCV可用于开发实时的图像处理、计算机视觉以及…

ctf刷题记录2(更新中)

因为csdn上内容过多编辑的时候会很卡&#xff0c;因此重开一篇&#xff0c;继续刷题之旅。 NewStarCTF 2023 WEEK3 Include &#x1f350; <?phperror_reporting(0);if(isset($_GET[file])) {$file $_GET[file];if(preg_match(/flag|log|session|filter|input|data/i, $…

解锁金融数据中心场景,实现国产化AD替代,宁盾身份域管为信创电脑、应用提供统一管理

随着信创国产化改造持续推进&#xff0c;越来越多的金融机构不断采购信创服务器、PC、办公软件等&#xff0c;其 IT 基础设施逐渐迁移至国产化 IT 架构下。为支撑国产化 IT 基础设施的正常使用和集中管理运维&#xff0c;某金融机构数据中心的微软Active Directory&#xff08;…

二、GitLab相关操作

GitLab相关操作 一、组、用户、项目管理1.创建组2.创建项目3.创建用户并分配组3.1 创建用户3.2 设置密码3.3 给用户分配组 二、拉取/推送代码1.配置ssh(第一次需要)1.1 创建一个空文件夹1.2 配置本地仓账号和邮箱1.3 生成ssh公钥密钥1.4 gitlab配置公钥 2.拉取代码3.推送代码3.…

FastAPI Web框架教程 第10章 APIRouter

10-1 APIRouter基本使用 需求场景 如果我们写一个网站&#xff0c;或者写一个APP&#xff0c;那整个项目应该是比较复杂的&#xff0c;此时不应该把所有代码放在一个文件中。 前几节课&#xff0c;我们通过把代码拆分到不同文件的方式&#xff0c;可以解决一些代码混乱的问题…

SD-WAN帮助企业实现对分布式网络的集中管理和控制

在当今数字化时代&#xff0c;企业网络越来越分散和复杂&#xff0c;分布在全球不同地点的分支机构和远程办公地点需要高效的网络连接来支持业务运营。传统的广域网&#xff08;WAN&#xff09;架构已经无法满足企业对网络灵活性、可靠性和安全性的需求。而SD-WAN的出现为解决这…

Phpstorm配置Xdebug

步骤 1、先去官网找到对应的php xdebug的版本 2、配置phpstorm断点调试 网址&#xff1a;https://xdebug.org/ 查看php对应的xdebug版本&#xff1a;Xdebug: Support — Tailored Installation Instructions 1.1查看对应php xdebug版本 全选&#xff0c;复制到目标网址 我…

C++要点细细梳理(上)(函数与面向对象)

之前我们讨论了C语言一些基础的细节&#xff0c;下面我们开始讨论C&#xff0c;&#xff0c;后面我打算接着谈C&#xff0c;也就是C#&#xff0c;先在此留个坑。 注意&#xff0c;本文有素材来自中国大学MOOC的C课程&#xff0c;本文也是该课程的听课笔记【这是链接】 1. 从C…

【HarmonyOS】ArkUI - 动画

利用属性动画、显示动画、组件转场动画实现组件动画效果。 一、属性动画 属性动画是通过设置组件的 animation 属性来给组件添加动画&#xff0c;当组件的 width、height、Opacity、backgroundColor、scale、rotate、translate 等属性变更时&#xff0c;可以实现渐变过渡效果。…

模块化编程:AMD 和 CMD 的魅力

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

力扣刷题部分笔记

Leetcode 力扣刷题笔记&#xff0c;记录了几个月来的题目记录&#xff0c;将会继续保持刷题~ 2024.01 1768.交替合并字符串 创建字符串不需要声明长度&#xff08;动态分配内存&#xff09;&#xff0c;push_back()可以加入某个字符&#xff0c;append()一般用于添加字符串…

js的qq换肤效果

文章目录 1. 演示效果2. 分析思路3. 代码实现3.1. 方式一3.2. 方式二3.3. 整体代码 1. 演示效果 2. 分析思路 先编写样式&#xff0c;弄好布局和排版。遍历这个集合&#xff0c;对每个图片元素&#xff08;img&#xff09;添加一个点击事件监听器。可以使用 for 或者 forEach …

IT外包服务:企业数据资产化加速利器

随着数字化时代的兴起&#xff0c;数据成为企业最为重要的资源之一。数据驱动创新对于企业的竞争力和可持续发展至关重要。在这一进程中&#xff0c;IT外包服务发挥着关键作用&#xff0c;加速企业数据资产化进程&#xff0c;为企业提供了重要支持。 首先&#xff0c;IT外包服务…

c++11 标准模板(STL)本地化库 - 平面类别 - (std::ctype) 定义字符分类表(四)

本地化库 本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析&#xff0c;以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C 标准库的其他组件的行为。 平面类别 定义字符分类表 std::ctype template< class CharT > clas…

计算机网络(五) 传输层

传输层 一、传输层概述二、TCP1.报文段格式2.连接管理3.可靠传输4.流量控制5.拥塞控制 三、UDP1.报文段格式2.校验 一、传输层概述 从通信和信息处理的角度看&#xff0c;传输层向它上面的应用层提供通信服务&#xff0c;它属于面向通信部分的最高层&#xff0c;同时也是用户功…

微信小程序纯CSS实现好看的加载动画

进入下面小程序可以体验效果&#xff1a; WXML: <wxs module"img" src"./loading.wxs"></wxs> <view class"loading-container {{show?:loading-container-hide}}"><view class"loading-mask" wx:if"{{ma…

LNMP环境:揭秘负载均衡与高可用性设计

lb1: 192.168.8.5 lb2: 192.168.8.6 web1:192.168.8.7 web2:192.168.8.8 php-fpm: 192.168.8.9 mysql: 192.168.8.10 nfs:192.168.8.11 分别插入镜像 8.5-8.8 分别安装nginx,并设置启动 8.9 安装php 8.10 安装mysql 先配置一台web服务器然后同步 设置网站根目录 cp -…

WebGL BabylonJS GUI 如何创建连接模型的按钮

如图所示&#xff1a; 方法&#xff1a; createGUI(mesh: BABYLON.Mesh, title: string, index: number) {const advancedTexture AdvancedDynamicTexture.CreateFullscreenUI(UI)const rect new Rectangle()rect.width 100pxrect.height 40pxrect.thickness 0advancedT…

在CentOS 8.5.2111下安装vncserver tigervnc-server

# 参考&#xff1a; How to Install TigerVNC Server on CentOS 8 前提&#xff1a; 默认用root操作所有命令 安装桌面GUI dnf groupinstall "Server with GUI" 安装tigervnc-server dnf install tigervnc-server 增加vncuser用户&#xff08;这里默认就是vncuse…

刷题之Leetcode704题(超级详细)

704. 二分查找 力扣题目链接(opens new window)https://leetcode.cn/problems/binary-search/ 给定一个 n 个元素有序的&#xff08;升序&#xff09;整型数组 nums 和一个目标值 target &#xff0c;写一个函数搜索 nums 中的 target&#xff0c;如果目标值存在返回下标&am…