初阶数据结构:链表相关题目练习(补充)

目录

  • 1. 单链表相关练习题
    • 1.1 移除链表元素
    • 1.2 反转链表
    • 1.3 链表的中间结点
    • 1.4 链表的倒数第k个结点
    • 1.5 合并两个有序链表
    • 1.6 链表分割
    • 1.7 链表的回文结构
    • 1.8 相交链表
    • 1.9 判断一个链表中是否有环
    • 1.10 寻找环状链表相遇点
    • 1.11 链表的深度拷贝

1. 单链表相关练习题

注:单链表结构上存在一定缺陷,所以链表相关的题目一般都针对与单链表。

1.1 移除链表元素

题目要求:在这里插入图片描述
题目信息:

  1. 头节点head
  2. 移除值val

题目链接:
移除链表元素

方法(顺序处理法):

思路:分析链表结构与结点所处的位置(是否为空链表,结点是否为头结点),分情况依次处理。

过程演示:
在这里插入图片描述

struct ListNode* removeElements4(struct ListNode* head, int val)
{struct ListNode* pre = NULL;struct ListNode* cur = head;while (cur){if (cur->val == val){//头删if (cur == head){head = head->next;free(cur);cur = head;}else//中间删{pre->next = cur->next;free(cur);cur = pre->next;}}else{pre = cur;cur = cur->next;}}return head;
}

1.2 反转链表

题目要求:
在这里插入图片描述
题目链接:
反转链表

过程演示:

struct ListNode* reverseList(struct ListNode* head) 
{struct ListNode* pre = NULL;struct ListNode* mid = head;struct ListNode* cur = NULL;if(head){cur = head->next;}while(mid){mid->next = pre;pre = mid;mid = cur;if(cur){cur = cur->next;}}return pre;}

1.3 链表的中间结点

题目要求:
在这里插入图片描述
题目链接:
链表的中间结点

过程演示(快慢指针法):

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

1.4 链表的倒数第k个结点

题目要求:
在这里插入图片描述
题目链接:
倒数第k个结点

过程演示:

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) 
{struct ListNode* cur = pListHead;struct ListNode* pre = pListHead;while(cur && k--){cur = cur->next;}if(k > 0){pre = NULL;}while(cur){pre = pre->next;cur = cur->next;}return pre;
}

1.5 合并两个有序链表

题目要求:
在这里插入图片描述
题目链接:
合并两个链表

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{struct ListNode* newhead = NULL;struct ListNode* cur = NULL;struct ListNode* newnode = NULL;if(list1 == NULL)return list2;if(list2 == NULL)return list1;while(list1 != NULL && list2 != NULL){if(list1->val <= list2->val){newnode = list1;list1 = list1->next;}else{newnode = list2;list2 = list2->next;}if(newhead == NULL){newhead = newnode;newnode->next = NULL;cur = newhead;}else{//在遍历过程中,list1 或 list2 会等于 NULLcur->next = newnode;if(newnode != NULL){newnode->next = NULL;cur = cur->next;}}}//有一个链表本就为空if(list1){cur->next = list1;}if(list2){cur->next = list2;}return newhead;
}

1.6 链表分割

题目要求:
在这里插入图片描述
题目链接:
合并两个链表

ListNode* BuyNewNode(int val){ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));newnode->val = val;newnode->next = nullptr;return newnode;}ListNode* partition(ListNode* pHead, int x) {ListNode* newhead1 = nullptr;ListNode* end1 = nullptr;ListNode* newhead2 = nullptr;ListNode* end2 = nullptr;ListNode* cur = pHead;while(cur){if(cur->val < x){if(newhead1 == nullptr){newhead1 = BuyNewNode(cur->val);end1 = newhead1;             }else {end1->next = BuyNewNode(cur->val);end1 = end1->next;}}else {if(newhead2 == nullptr){newhead2 = BuyNewNode(cur->val);end2 = newhead2;             }else {end2->next = BuyNewNode(cur->val);end2 = end2->next;}}cur = cur->next;}if(newhead1 == nullptr){newhead1 = newhead2;}else {end1->next = newhead2;}return newhead1;}

1.7 链表的回文结构

题目要求:
在这里插入图片描述
题目链接:
回文串

ListNode* reverse(ListNode* head){ListNode* pre = nullptr;ListNode* mid = head;ListNode* cur = head->next;while (mid){mid->next = pre;pre = mid;mid = cur;if (cur){cur = cur->next;}}return pre;}bool chkPalindrome(ListNode* A){//找相同,逆置//逐点比较//回文结构存在奇数个结点//获取中间结点ListNode* fast = A;ListNode* slow = A;while(fast && fast->next){fast = fast->next->next;if(fast){slow = slow->next;}}//表1ListNode* newhead2 = slow->next;//表2slow->next = nullptr;ListNode* newhead1 = reverse(A);if(fast){newhead1 = newhead1->next;}while (newhead1 && newhead2 && newhead1->val == newhead2->val){newhead1 = newhead1->next;newhead2 = newhead2->next;}if (newhead1 == nullptr && newhead2 == nullptr){return true;}return false;}

1.8 相交链表

题目要求:
在这里插入图片描述题目链接:
相交链表

void swap(struct ListNode** node1, struct ListNode** node2)
{struct ListNode* tmp = *node1;*node1 = *node2;*node2 = tmp;
}struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{struct ListNode* short_list1 = headA;struct ListNode* short_list2 = headA;struct ListNode* long_list1 = headB;struct ListNode* long_list2 = headB;while(short_list1 && long_list1){short_list1 = short_list1->next;long_list1 = long_list1->next;}if(short_list1){swap(&short_list1, &long_list1);swap(&short_list2, &long_list2);}while(long_list1){long_list1 = long_list1->next;long_list2 = long_list2->next;}//while((short_list2 && long_list2) || short_list2 != long_list2)while(short_list2 && long_list2 && short_list2 != long_list2){long_list2 = long_list2->next;short_list2 = short_list2->next;}return short_list2;
}

1.9 判断一个链表中是否有环

题目要求:
在这里插入图片描述
题目链接:
判断是否有环

//逻辑步骤存疑
bool hasCycle(struct ListNode *head) 
{struct ListNode* fast = head;struct ListNode* slow = head;//err: while(fast && slow != fast)while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(slow == fast){return true;}}return false;
}

1.10 寻找环状链表相遇点

题目要求:
在这里插入图片描述
题目链接:
寻找环状链表相遇点

思路依靠结论:一个结点从起始点,一个结点从相遇点(快慢指针相遇点),以同速行走(一次走一步),当他们再一次初次相遇时,此相遇结点即为入环点。

struct ListNode *detectCycle(struct ListNode *head) 
{//快慢指针确定首次相遇点//起始点与相遇点出发,同速移动,最后的相遇的点即为环的进入点struct ListNode* fast = head;struct ListNode* slow = head;//遍历寻找相遇点while(fast && fast->next){fast = fast->next->next;slow = slow->next;if(fast == slow){break;}}//判断是否为环状链表if(fast == NULL || fast->next == NULL){return NULL;}slow = head;while(fast != slow){fast = fast->next;slow = slow->next;}return fast;
}

1.11 链表的深度拷贝

题目要求:
在这里插入图片描述>题目链接:
链表的深度拷贝

//思路:
//生成拷贝结点
//调整拷贝结点的指针
//还原原链表,链接拷贝结点
struct Node* BuyNewNode(int val)
{struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));newnode->val = val;newnode->next = NULL;newnode->random = NULL;return newnode;
}struct Node* copyRandomList(struct Node* head)
{struct Node* node1 = head;//判断是否为空链表if(head == NULL){return head;}//创建新节点while (node1){struct Node* newnode = BuyNewNode(node1->val);newnode->next = node1->next;node1->next = newnode;node1 = node1->next->next;}//调整新节点的随机指针struct Node* node2 = head->next;node1 = head;while (node2){if (node1->random){node2->random = node1->random->next;}node1 = node1->next->next;if (node2->next){node2 = node2->next->next;}else{node2 = node2->next;}}//还原链表,链接新链表node1 = head;node2 = head->next;struct Node* newhead = head->next;while (node1){node1->next = node1->next->next;if (node2->next){node2->next = node2->next->next;}node1 = node1->next;node2 = node2->next;}return newhead;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IEEE Transactions on Industrial Electronics工业电子TIE修改稿注意事项及提交须知

一、背景 兔年末投了一篇TIE&#xff0c;手稿初次提交的注意事项也整理成了博客IEEE Transactions on Industrial Electronics工业电子TIE论文投稿须知&#xff0c;获得了许多点赞和收藏。最近也收到了审稿结果&#xff0c;给的意见是大修major revision&#xff0c;总之只要不…

基于springboot+vue的线上辅导班系统

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

吸猫毛空气净化器哪个好?推荐除猫毛好的宠物空气净化器品牌

如今&#xff0c;越来越多的家庭选择养宠物&#xff01;虽然家里变得更加温馨&#xff0c;但养宠可能会带来异味和空气中的毛发增多可能会引发健康问题&#xff0c;这也是一个大问题。 但我不想家里到处都是异味&#xff0c;尤其是便便的味道&#xff0c;所以很需要一款能够处…

QML中表格中数据获取

1.在生成的动态表格中获取某格数据的内容 import QtQuick 2.15 import QtQuick.Window 2.15import QtQuick.Controls 2.0 import Qt.labs.qmlmodels 1.0 import QtQuick.Layouts 1.15Window {width: 640height: 480visible: truetitle: qsTr("Hello World")TableMod…

数据分析-Pandas数据如何图示规律

数据分析-Pandas数据如何图示规律 数据分析和处理中&#xff0c;难免会遇到各种数据&#xff0c;那么数据呈现怎样的规律呢&#xff1f;不管金融数据&#xff0c;风控数据&#xff0c;营销数据等等&#xff0c;莫不如此。如何通过图示展示数据的规律&#xff1f; 数据表&…

VS2015报错:error MSB8020和MSB8036的解决方案

VS2015编译报错&#xff1a;error MSB8020 提示信息&#xff1a;error MSB8020: The build tools for v141 (Platform Toolset ‘v141’) cannot be found. To build using the v141 build tools, please install v141 build tools. Alternatively, you may upgrade to the c…

小程序框架接口-getApp

框架接口-getApp getApp() 用于获取小程序全局唯一的 App 实例&#xff0c;通过小程序应用实例可实现数据或方法的共享 &#x1f4cc; 注意事项&#xff1a; 1.不要在 App() 方法中使用 getApp() &#xff0c;使用 this 就可以拿到 app 实例通过 getApp() 获取实例之后&#x…

Android13 Audio框架

一、Android 13音频代码结构 1、framework: android/frameworks/base 1.AudioManager.java &#xff1a;音频管理器&#xff0c;音量调节、音量UI、设置和获取参数等控制流的对外API 2.AudioService.java &#xff1a;音频系统服务&#xff08;java层&#xff09;&#xff0c…

多模态论文阅读-LLaVA

Visual Instruction Tuning Abstract1. Introduction2. Related Work3. GPT-assisted Visual Instruction Data Generation4. Visual Instruction Tuning4.1 Architecture4.2 Training 5 Experiments5.1 Multimodal Chatchot5.2 ScienceQA 6 Conclusion Abstract 使用机器生成…

JS中判断是否存在逗号,如果存在给去掉

.includes() 方法判断是否存在 split("需要去掉的字符串").join(" ") 去重的方法 去重复 划分后拼接

网络——DHCP服务器、DNS服务器实验

网络——DHCP服务器、DNS服务器实验 一、DHCP服务器实验 DHCP——动态主机配置协议,用来管理ip地址的分配。网络中的每台计算机都有至少一个ip地址。在Windows网络连接对话框中可以设置成自动获取ip地址,这样主机作为DHCP client就可以自动从DHCP server获取ip地址了。 DHC…

live555学习 - 环境准备

环境&#xff1a;Ubuntu 16.04.7 ffmpeg-6.1 1 代码下载 最新版本&#xff1a; http://www.live555.com/liveMedia/public/ 历史版本下载 https://download.videolan.org/pub/contrib/live555/ 选择版本live.2023.01.19.tar.gz ps&#xff1a;没有选择新版本是新版本在…

数据库优化建议

尽量控制单表数据量的大小&#xff0c;建议控制在 500 万以内 500 万并不是 MySQL 数据库的限制&#xff0c;过大会造成修改表结构&#xff0c;备份&#xff0c;恢复都会有很大的问题。可以用历史数据归档&#xff08;应用于日志数据&#xff09;&#xff0c;分库分表&#xf…

阿里开源的Java诊断利器Arthas

一.什么是Arthas 1.为什么需要Arthas 通常&#xff0c;本地开发环境无法访问生产环境。如果在生产环境中遇到问题&#xff0c;则无法使用 IDE 远程调试。更糟糕的是&#xff0c;在生产环境中调试是不可接受的&#xff0c;因为它会暂停所有线程&#xff0c;导致服务暂停。 开…

探索Apple Vision Pro:创新技术带来的多彩应用世界

Apple Vision Pro是一款具有前沿技术的设备,可以与现实世界进行交互,让用户在虚拟世界中享受各种应用。以下是一些值得注意的Vision Pro应用: AR演示环境:Vision Pro上的AR应用主要是基于AR的演示环境,这些应用可以让用户在现实世界中体验虚拟世界。游戏:Vision Pro上有一…

c语言统计字符

本题要求编写程序&#xff0c;输入10个字符&#xff0c;统计其中英文字母、空格或回车、数字字符和其他字符的个数。 输入格式: 输入为10个字符。最后一个回车表示输入结束&#xff0c;不算在内。 输出格式: 在一行内按照 letter 英文字母个数, blank 空格或回车个数, d…

鸿蒙Harmony应用开发—ArkTS声明式开发(鼠标事件)

在鼠标的单个动作触发多个事件时&#xff0c;事件的顺序是固定的&#xff0c;鼠标事件默认透传。 说明&#xff1a; 从API Version 8开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。目前仅支持通过外接鼠标触发。 onHover onHover(event: …

vue中element-ui中的el-button自定义icon图标

实现&#xff1a; button的icon属性自定义一个图标名称&#xff0c;这个自定义的图标名称会默认添加到button下i标签的class上&#xff0c;我们只需要设置i标签的样式就可以了。 1. 控制台显示的代码 2 .图片展示 3. 按钮上使用自定义的icon 完整代码&#xff1a; <el-but…

postman切换成黑色主题

postman安装以后默认是白色背景&#xff0c;如果想要切换成黑色的&#xff0c;大家可以按照下图箭头指示来操作。 1打开设置 2在Themes页面选择黑色主题

物联网常见协议篇

在物联网环境中&#xff0c;物联网协议承担着关键作用&#xff0c;而新手了解物联网协议如传输协议、通讯协议和行业协议等。 一、物联网协议 物联网协议是物联网环境中的关键组成部分&#xff0c;它承担着设备间通信和数据传输的重要任务。这些协议根据其作用的不同&#xff…