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

数据结构之链表笔试题详解

一:移除链表元素

我们很容易就可以想到一个解决方案:再创建一个链表,把不是val的结点拿过来尾插。

这样确实可以但是,我们每次尾插都需要遍历一遍整个链表,这样时间复杂度就变成了O(n^2),

因此我们不妨设置一个tail尾结点来指向它的尾。

画图分析

这里面还有一个潜在的问题就是,假如最后一个节点的数值==val怎么办,想一想。

完整代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {ListNode* newhead;ListNode* newtail;ListNode* pcur = head;newhead=newtail=NULL;while(pcur){if(pcur->val!=val){if(newhead==NULL){newhead=newtail=pcur;}else{newtail->next=pcur;newtail=pcur;}}pcur=pcur->next;}if(newtail!=NULL&&newtail->next!=NULL){newtail->next=NULL;}return newhead;
}

二:反转链表

这一题有两种解法:

解法一:

直接反转指向,比如原来二指向三,现在让三指向2.

画图分析:

经过分析我们写出了这样的代码:

struct ListNode* reverseList(struct ListNode* head) {

    struct ListNode* pprev = NULL;

    struct ListNode* pcur = head;

    struct ListNode* pnext = head->next;

    while(pcur)

    {

        pcur->next=pprev;

        pprev=pcur;

        pcur=pnext;

        pnext=pnext->next;

    }

    return pprev;

}

放在力扣上提交

遇到错误不要慌张,我们看一下提示信息,可以发现,原来是对NULL解引用了,所以在17行那要加上判断,那么说到这了万一这个链表原来就是空呢?因此在一开始也要对这种情况处理。

完整代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* reverseList(struct ListNode* head) {struct ListNode* pprev = NULL;struct ListNode* pcur = head;struct ListNode* pnext;if(head){pnext = head->next;}while(pcur){pcur->next=pprev;pprev=pcur;pcur=pnext;if(pnext){pnext=pnext->next;}}return pprev;
}

解法二:

取原链表中元素,头插到新链表的newhead中。

画图分析:

完整代码:

struct ListNode* reverseList(struct ListNode* head) {struct ListNode* pcur = head;struct ListNode* newhead = NULL;struct ListNode* pnext;if(head){pnext=head->next;}while(pcur){pcur->next = newhead;newhead = pcur;pcur = pnext;if(pnext){pnext = pnext->next;}}return newhead;
}

三:链表的倒数k个结点

我们确实可以先遍历一遍拿到链表长度,然后就可以找到倒数k个结点了。但是如果只能这么做就没有拿出来的必要了qwq。

快慢指针法:两个指针先让一个走k,然后一起走,等到k指向空(具体看下面画图分析)结束。

画图分析:

完整代码:

struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) {struct ListNode* slow;struct ListNode* fast;slow=fast=pHead;while(k--){if(fast){fast=fast->next;}else {return NULL;}}while(fast){slow=slow->next;fast=fast->next;}return slow;
}

四:链表的中间结点

这题同样也是快慢指针法,大体思路是让fast走两步,slow走一步,但是直觉告诉我们结点数的奇偶性会影响结束条件。

画图分析:

完整代码:

struct ListNode* middleNode(struct ListNode* head) {struct ListNode* slow;struct ListNode* fast;fast=slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}return slow;
}

五:合并两个有序链表

思路:依次比较链表中的结点,然后插入小的结点

画图分析:

完整代码:

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {struct ListNode* newhead;struct ListNode* newtail;newhead=newtail=NULL;struct ListNode* pcur1=list1;struct ListNode* pcur2=list2;if(pcur1==NULL)return pcur2;if(pcur2==NULL)return pcur1;while(pcur1&&pcur2){if(pcur1->val<pcur2->val){if(newhead==NULL){   newhead=newtail=pcur1;}else{newtail->next=pcur1;newtail=pcur1;}pcur1=pcur1->next;}else{if(newhead==NULL){   newhead=newtail=pcur2;}else{newtail->next=pcur2;newtail=pcur2;}pcur2=pcur2->next;}}if(pcur1!=NULL){newtail->next=pcur1;}if(pcur2!=NULL){newtail->next=pcur2;}return newhead;
}


优化:

我们每次都要对新创建的头结点判断是否为空。

有没有一种方法可以不用判断呢?:设置一个哨兵位就可以了

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {struct ListNode* pcur1=list1;struct ListNode* pcur2=list2;if(pcur1==NULL)return pcur2;if(pcur2==NULL)return pcur1;struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* tail = head;while(pcur1&&pcur2){if(pcur1->val<pcur2->val){tail->next=pcur1;tail=pcur1;pcur1=pcur1->next;}else{tail->next=pcur2;tail=pcur2;pcur2=pcur2->next;}}if(pcur1!=NULL){tail->next=pcur1;}else{tail->next=pcur2;}struct ListNode* newhead=head->next;free(head);return newhead;
}

六:链表分割

这题没给图,直接画图分析:

画图分析:

完整代码:

class Partition {
public:ListNode* partition(ListNode* pHead, int x) {ListNode* lesshead =(ListNode*)malloc(sizeof(ListNode));ListNode* lesstail=lesshead;ListNode* greaterhead=(ListNode*)malloc(sizeof(ListNode));ListNode* greatertail=greaterhead;ListNode* pcur=pHead;if(pcur==NULL)return NULL;while(pcur){if(pcur->val<x){lesstail->next=pcur;lesstail=pcur;}else {greatertail->next=pcur;greatertail=pcur;}pcur=pcur->next;}lesstail->next=greaterhead->next;greatertail->next=NULL;ListNode* newhead=lesshead->next;free(lesshead);free(greaterhead);return newhead;}
};

七:链表回文

首先这一题开一个900的数组也可以过,但是他并不符合空间复杂度O(1)。

然后我们说一下这一题的思路:先找中间结点,然后逆置,最后一一比较。

具体细节,请看下

画图分析:

完整代码:

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
struct ListNode* middleNode(ListNode* A)
{ListNode* slow = A;ListNode* fast = A;while(fast && fast->next){fast=fast->next->next;slow=slow->next;}return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{struct ListNode* pprev = NULL;struct ListNode* pcur = head;struct ListNode* pnext;if(head){pnext = head->next;}while(pcur){pcur->next = pprev;pprev = pcur;pcur = pnext;if(pnext){pnext = pnext->next;}}return pprev;
}
class PalindromeList {
public:bool chkPalindrome(ListNode* A) {ListNode* mid = middleNode(A);ListNode* rhead = reverseList(mid);ListNode* curA = A;ListNode* curR = rhead;while(curA && curR){if(curA->val != curR->val){return false;   }else{curA=curA->next;curR=curR->next;}}return true;}
};

八:链表相交

思路:遍历一遍求长度(同时判断是否会相交),长的走差值步,然后一起走直到两指针相同

这一题不可以用逆置做,想一想为什么??

画图分析:

完整代码:


struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode* curA = headA;struct ListNode* curB = headB;int lenA = 1; int lenB = 1;while(curA->next){lenA++;curA=curA->next;}while(curB->next){lenB++;curB=curB->next;}if(curA!=curB)return NULL;int gap = abs(lenA-lenB);struct ListNode* longlist = headA;struct ListNode* shortlist = headB;if(lenA < lenB){longlist = headB;shortlist = headA;}while(gap--){longlist=longlist->next;}while(longlist!=shortlist){longlist=longlist->next;shortlist=shortlist->next;}return longlist;
}

九:环形链表1

思路:快慢指针法,slow走1步fast走2步。如果相遇则带环,否则不带环

画图分析:

完整代码:

bool hasCycle(struct ListNode *head) {struct ListNode* slow = head;struct ListNode* fast = head;while(fast && fast->next){fast=fast->next->next;slow=slow->next;if(fast==slow)return true;}return false;
}

十:环形链表2

这一题有点数学题的意思。还是先说思路:还是先slow与fast走,等到两指针相遇,在再让一个指针从都走,另一个指针从相遇位置走,直到二者再次相遇即为如环点。

画图分析:

完整代码:

struct ListNode *detectCycle(struct ListNode *head) {struct ListNode* slow = head;struct ListNode* fast = head;while(fast && fast->next){fast=fast->next->next;slow=slow->next;if(slow==fast){struct ListNode* cur = head;struct ListNode* curC = fast;while(cur!=curC){cur=cur->next;curC=curC->next;}return cur;}}return NULL;
}

十一:复杂随机链表的拷贝

这一次最难搞的是random指针。思路也不好想,大家还是借鉴一下大佬的思路吧。

思路:在每个节点后面插入一个一样的节点,定义cur 和 next指针 cur->next->radom=cur->radom->next,然后迭代往后。最后解开插入的结点。

画图分析:

完整代码:


struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;//插入新节点while(cur){struct Node* newnode = (struct Node* )malloc(sizeof(struct Node));newnode->val = cur->val;newnode->next = cur->next;cur->next = newnode;cur = newnode->next;}//处理randomcur = head;while(cur){struct Node* newnode = cur -> next;if(cur->random==NULL){newnode->random = NULL;}else{newnode->random = cur->random->next;}cur = newnode->next;}//取新节点尾插,同时恢复原链表cur = head;struct Node* newhead = NULL;struct Node* newtail = NULL;while(cur){struct Node* newnode = cur->next;struct Node* next = newnode->next;if(newhead==NULL){newhead = newtail = cur->next;}else{newtail->next = newnode;newtail = newnode;}cur->next = newnode->next;cur = newnode->next;}return newhead;
}

相关文章:

数据结构之链表笔试题详解

一&#xff1a;移除链表元素 我们很容易就可以想到一个解决方案&#xff1a;再创建一个链表&#xff0c;把不是val的结点拿过来尾插。 这样确实可以但是&#xff0c;我们每次尾插都需要遍历一遍整个链表&#xff0c;这样时间复杂度就变成了O(n^2)&#xff0c; 因此我们不妨设…...

结构化的Prompt

资源库&#xff1a; AI 提示词-WayToAGI精选高效的AI提示词库&#xff0c;助力创作者和开发者解锁人工智能的潜力。通过我们的提示词和策略&#xff0c;优化您的AI工具使用效率&#xff0c;激发创意思维&#xff0c;提升产出质量。https://www.waytoagi.com/prompts?tag6 结构…...

【数字化】华为数字化转型架构蓝图

导读&#xff1a;华为的数字化转型规划团队在2016年年底基于对愿景的系统诠释&#xff0c;整合出了数字化转型架构蓝图。该蓝图共分为5层&#xff0c;旨在通过数字化转型实现客户交互方式的转变、作战方式的转变、公司各平台业务能力的数字化、服务化以及运营模式的转变。 目录…...

最新全开源IM即时通讯系统源码(PC+WEB+IOS+Android)部署指南

全开源IM&#xff08;即时通讯&#xff09;系统源码部署是一个复杂但系统的过程&#xff0c;涉及多个组件和步骤。以下是一个详细的部署指南&#xff0c;旨在帮助开发者或系统管理员成功部署一个全开源的IM系统&#xff0c;如OpenIM。      IM即时通讯系统源码准备工作   …...

go 跨平台打包

GOARCH‌是Go语言中的一个环境变量&#xff0c;用于指定目标平台的底层架构。在Go的交叉编译过程中&#xff0c;‌GOARCH‌决定了编译出的二进制文件将在哪种硬件架构上运行。 GOARCH的常见值 ‌amd64‌&#xff1a;64位 x86 架构‌386‌&#xff1a;32位 x86 架构‌arm‌&am…...

C++ 给定字符串,然后给出开始要取的位置,返回取到的信息

3 happy new year 7 year 1 new 4 new year year error input #include <stdio.h> #include <string.h> void strmcpy(char* s, char* t, int m); int main() {int repeat, m;char t[1000], s[1000];scanf("%d", &repeat);getchar(); //吸收换行符in…...

【树莓派4B】MindSpore lite 部署demo

一个demo&#xff0c;mindspore lite 部署在树莓派4B ubuntu22.04中&#xff0c;为后续操作开个门&#xff01; 环境 开发环境&#xff1a;wsl-ubuntu22.04分发版部署环境&#xff1a;树莓派4B&#xff0c;操作系统为ubuntu22.04mindspore lite版本&#xff1a;mindspore-li…...

Idea汉化插件Datagrip汉化插件

汉化插件 ‍ ‍ Chinese (Simplified) Language Pack / 中文语言包 ‍ 插件地址 ‍ 安装完了之后,如果还不是中文的怎么办 ‍ 需要手动设置 Seetings -> Appearance & Behavior -> System Settings -> Language and Region -> Language 修改为 [ Chi…...

精彩回顾|Cocos开发者沙龙长沙站

长沙-不一样 Cocos 开发者沙龙长沙站&#xff0c;完全超出了我们的预期&#xff0c;一开始还担心没有太多人报名。最后发现&#xff0c;全场爆满&#xff0c;座无虚席。 <<< 左右滑动见更多 >>> 许多小伙伴曾反馈过&#xff0c;在以往的开发者沙龙回顾文章中…...

算法日记 49 day 图论(A*算法)

这算是算法的最后一篇了&#xff0c;原本A*之前还有一些相关的最短路径算法的&#xff0c;比如dijkstra的堆优化&#xff0c;SPFA等等&#xff0c;但是有些我没看懂&#xff0c;就不写了&#xff0c;用A*做个结尾。 题目&#xff1a;骑士的攻击 127. 骑士的攻击 (kamacoder.co…...

服务器批量清理redis keys,无法适用客户端必须直连的情况

在 Redis 中&#xff0c;批量清理指定模式的键&#xff08;例如 memberCardData:*&#xff09;可以通过多种方法来实现。需要注意的是&#xff0c;Redis 的命令执行是单线程的&#xff0c;因此对大量键进行操作时可能会阻塞服务器。以下是几种常见的方法&#xff1a; shell K…...

Grafana配置告警规则推送企微机器人服务器资源告警

前提 已经部署Grafana&#xff0c;并且dashboard接入数据 大屏编号地址&#xff1a;Node Exporter Full | Grafana Labs 创建企微机器人 备注&#xff1a;群里若有第三方外部人员不能创建 机器人创建完成&#xff0c;记录下来Webhook地址 Grafana配置告警消息模板 {{ define &…...

数字货币金融研究,深度学习虚拟币价格预测 数据集 市值top20 (2014年—2024年)

比特币&#xff0c;以太坊&#xff0c;狗狗币&#xff0c;屎币&#xff0c;模因币 声明 此数据集的目的是 用于数字货币金融研究&#xff0c;深度学习虚拟币价格预测 1、数据集 2014年——2024年 市值top20 比特币&#xff0c;以太坊&#xff0c;屎币&#xff0c;狗狗币交易…...

druid.properties图标是齿轮

一、问题 在IDEA中&#xff0c; druid.properties图标是齿轮 二、原因 2023版本开始&#xff0c;IDEA新的UI的问题 三、解决方法 1、点击右上角的齿轮图标 2、点击Settings 3、Appearance & Behavior---->New UI---->取消勾选“Enable new UI”---->右下角OK 4…...

【图像处理】利用numpy、opencv、python实现车牌检测

| 利用opencv实现车牌检测 整体流程涉及5个部分 图像通道转换对比度增强边缘连接二值化边界区域裁剪 图像通道转换 将RGB图像转换为HSV图像&#xff0c;仅保留V通道。V通道表示颜色的明暗&#xff0c;常用于图像对比度拉伸、直方图均衡化等流程。 原图像&#xff1a; V通…...

ModuleNotFoundError: No module named ‘torchvision.transforms.functional_tensor‘

问题&#xff1a; 运行代码时&#xff0c;报错&#xff1a; … File “/home/xzy/anaconda3/envs/groundinggpt/lib/python3.10/site-packages/pytorchvideo/transforms/augmix.py”, line 6, in from pytorchvideo.transforms.augmentations import ( File “/home/xzy/anac…...

Android无障碍服务监听实现自动点击按钮

原理&#xff1a; 通过监听窗口改变事件&#xff0c;监听目标应用&#xff0c;通过视图ID&#xff08;或文本、或描述、或其他如坐标之类的&#xff09;找到目标视图&#xff0c;使用无障碍动作点击方法点击它 无障碍服务实现&#xff1a; 1、写一个自己的无障碍服务继承Acc…...

Deveco Studio首次编译项目初始化失败

编译项目失败 Ohpm install失败的时候重新使用管理者打开程序 build init 初始化失败遇到了以下报错信息 Installing pnpm8.13.1... npm ERR! code CERT_HAS_EXPIRED npm ERR! errno CERT_HAS_EXPIRED npm ERR! request to https://registry.npm.taobao.org/pnpm failed, r…...

Redis缓存应用场景【Redis场景上篇】

文章目录 1.缓存基础2.缓存异步场景1.缓存穿透2.缓存击穿3.缓存雪崩总结 3.缓存一致性 1.缓存基础 Redis由于性能高效&#xff0c;通常可以做数据库存储的缓存。一般而言&#xff0c;缓存分为服务端缓存和客户端缓存。缓存有以下三种模式&#xff1a; Cache Aside&#xff08…...

线程与进程基础

文章目录 前言一、 线程与进程1.1 什么是线程与进程&#xff1f;1.2 并发与并行1.3 同步调用与异步调用1.4 为什么要使用多线程&#xff1f; 前言 在学习juc前&#xff0c;需要先对进程和线程之间整体有一个认知。我们之前或多或少接触过&#xff0c;一些特别高大上的概念&…...

AI大模型核心:Prompt、Tool、Skill、Agent,一篇彻底搞懂它们之间的区别与实战应用!

如果你最近在用AI大模型&#xff0c;一定会被这四个词绕晕&#xff1a;Prompt、Tool、Skill、Agent。 这篇文章用最通俗的语言&#xff0c;一次性讲透四个概念的本质、核心区别。一、讲清楚每个概念到底是什么&#xff1f; 1、Prompt 本质上是人类给大模型的单次文本指令&#…...

snnTorch NIR导出功能详解:实现跨框架模型转换

snnTorch NIR导出功能详解&#xff1a;实现跨框架模型转换 【免费下载链接】snntorch Deep and online learning with spiking neural networks in Python 项目地址: https://gitcode.com/gh_mirrors/sn/snntorch snnTorch是一个基于Python的脉冲神经网络&#xff08;SN…...

英伟达市值“富可敌国”,AI基建核心地位稳固但仍有隐忧

英伟达市值惊人&#xff0c;超多数国家经济体截至2026年5月21日&#xff0c;英伟达的市值大约在5.5万亿美元。据悉&#xff0c;按IMF 2026年4月版《世界经济展望》的名义GDP预测&#xff0c;美国约32.38万亿美元&#xff0c;中国约20.85万亿美元&#xff0c;德国约5.45万亿美元…...

终极AI评估指南:用DeepEval开源框架轻松保障你的大语言模型质量

终极AI评估指南&#xff1a;用DeepEval开源框架轻松保障你的大语言模型质量 【免费下载链接】deepeval The LLM Evaluation Framework 项目地址: https://gitcode.com/GitHub_Trending/de/deepeval 你是否曾担心AI助手给出错误的医疗建议&#xff1f;是否焦虑金融AI客服…...

如何打破闭源代码智能模型的垄断?DeepSeek-Coder-V2的技术突围与实践指南

如何打破闭源代码智能模型的垄断&#xff1f;DeepSeek-Coder-V2的技术突围与实践指南 【免费下载链接】DeepSeek-Coder-V2 DeepSeek-Coder-V2: Breaking the Barrier of Closed-Source Models in Code Intelligence 项目地址: https://gitcode.com/GitHub_Trending/de/DeepSe…...

长期使用后回顾 Taotoken 在 API 调用稳定性与客服响应上的综合体验

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 长期使用后回顾 Taotoken 在 API 调用稳定性与客服响应上的综合体验 作为一项服务于项目开发的基础设施&#xff0c;大模型 API 的…...

2026 年 Haskell 基金会大变革:执行董事卸任、组织重组、董事会人员调整!

执行董事卸任过去几年担任执行董事的 Jos 决定在 2026 年 6 月卸任。Jos 是 Haskell 基金会任职时间最长的执行董事&#xff0c;他花费大量时间与社区互动并提供支持&#xff0c;很多工作都是在幕后默默完成的。Jos 做出了个人牺牲&#xff0c;让 Haskell 基金会度过了艰难时期…...

编程语言对比:从C到Python

好的&#xff0c;我将为你清晰介绍这几种编程语言的主要区别&#xff1a;1. C语言定位&#xff1a;面向过程的系统级编程语言。特点&#xff1a;接近硬件&#xff0c;可直接操作内存&#xff08;如指针&#xff09;。语法简洁&#xff0c;无面向对象特性。应用场景&#xff1a;…...

毕业设计 深度学习动物识别系统(源码+论文)

文章目录 0 前言1 项目运行效果1 背景2 算法原理2.1 动物识别方法概况2.2 常用的网络模型2.2.1 B-CNN2.2.2 SSD 3 SSD动物目标检测流程4 实现效果5 部分相关代码5.1 数据预处理5.2 构建卷积神经网络5.3 tensorflow计算图可视化5.4 网络模型训练5.5 对猫狗图像进行2分类 6 最后 …...

2026数字营销岗位需要具备的能力有哪些

数字营销这几年变化很快&#xff0c;到了2026年&#xff0c;岗位要求已经不再只是“会投放、会写文案、会做表格”这么简单了。很多职场人都能明显感觉到&#xff1a;过去靠经验拍脑袋做营销&#xff0c;越来越难&#xff1b;未来真正有竞争力的人&#xff0c;往往是那些既懂业…...