数据结构学习——链表面试题

1. 删除链表中等于给定值 val 的所有结点。

203. 移除链表元素 - 力扣(LeetCode)

方法一:

struct ListNode* removeElements(struct ListNode* head, int val) 
{struct ListNode* prev=NULL;struct ListNode* cur=head;while(cur){if(cur->val==val){struct ListNode* next = cur->next;free(cur);if(prev)prev->next=next;elsehead = next;cur=next;}else{prev=cur;cur=cur->next;}}return head;
}

在这里插入图片描述

方法二:

哨兵位的头节点不存储有效数据

struct ListNode* removeElements(struct ListNode* head, int val) 
{struct ListNode* newhead = NULL,*tail=NULL;struct ListNode* cur=head;//哨兵位newhead =tail=(struct ListNode*)malloc(sizeof(struct ListNode));while(cur){//不是val的节点拿下来尾插if(cur->val !=val){tail->next=cur;tail=tail->next;cur=cur->next;}else {struct LIstNode* tmp= cur;cur=cur->next;free(tmp);}}tail->next =NULL;struct ListNode* tmp  =newhead;newhead=newhead->next;free(tmp);return newhead;
}

2.反转一个单链表。

206. 反转链表 - 力扣(LeetCode)

struct ListNode* reverseList(struct ListNode* head) 
{if(head==NULL)retuen NULL;struct ListNode* n1 ,*n2.n3;n1 = NULL;n2 = head;n3 = head->next;while(n2){n2->next=n1;n1=n2;n2=n3if(n3)n3=n3->next;}return n1;
}

在这里插入图片描述

3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则 返回第二个中间结点。

876. 链表的中间结点 - 力扣(LeetCode)

struct ListNode* middleNode(struct ListNode* head)
{struct ListNode*prev=head,*tail=head;while(tail && tail->next){prev=prev->next;tail=tail->next->next;}return prev;
}

在这里插入图片描述

4. 输入一个链表,输出该链表中倒数第k个结点。

面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)

int kthToLast(struct ListNode* head, int k)
{struct ListNode*cur=head;while(cur){if(cur->val==k){return cur->val;}cur=cur->next;}return NULL;
}

在这里插入图片描述

5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有 结点组成的。

21. 合并两个有序链表 - 力扣(LeetCode)

typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{if(list1==NULL)return list2;if(list2==NULL)return list1;ListNode* phead1 ,*phead2 ;ListNode* phead3 ,*cur;phead3 = cur = (ListNode*)malloc(sizeof(ListNode));phead1 = list1; phead2 = list2;while(phead1 && phead2){if(phead1->val <= phead2->val){cur->next = phead1;phead1 = phead1->next;}else{cur->next = phead2;phead2 = phead2->next;}cur = cur->next;}if(phead1){cur->next = phead1;}if(phead2){cur->next = phead2;}phead1 = phead3->next;free(phead3);return phead1;
}

在这里插入图片描述

6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。

链表分割_牛客题霸_牛客网 (nowcoder.com)

class Partition {
public:ListNode* partition(ListNode* pHead, int x) {// write code herestruct ListNode *head1,*tail1,*head2,*tail2;head1 = tail1 = (struct ListNode*) malloc(sizeof(struct ListNode));head2 = tail2 = (struct ListNode*) malloc(sizeof(struct ListNode));struct ListNode *cur = pHead;while(cur){if(cur->val <x){tail1->next = cur;tail1 = tail1->next;}else{tail2->next = cur;tail2 = tail2->next;}cur=cur->next;}tail1->next = head2->next;tail2->next = NULL;pHead = head1->next;free(head1);free(head2);return pHead;}
};

在这里插入图片描述

7. 链表的回文结构。

链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

class PalindromeList {
public:struct ListNode* reverseList(struct ListNode* head) 
{if(head == NULL)return NULL;struct ListNode* n1 ,*n2,*n3;n1 = NULL;n2 = head;n3 = head->next;while(n2){n2->next=n1;n1=n2;n2=n3;if(n3)n3=n3->next;}return n1;
}struct ListNode* middleNode(struct ListNode* head)
{struct ListNode*prev=head,*tail=head;while(tail && tail->next){prev=prev->next;tail=tail->next->next;}return prev;
}bool chkPalindrome(ListNode* head) {// write code herestruct ListNode* mid = middleNode(head);struct ListNode* rhead = reverseList(head); while(head && rhead){if(head->val != rhead->val){return false;}head = head->next;rhead = rhead ->next;}return true;}};

思路:先找中间,再逆置,然后比较

在这里插入图片描述

8. 环形链表I

141. 环形链表 - 力扣(LeetCode)

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

在这里插入图片描述

9.环形链表II

142. 环形链表 II - 力扣(LeetCode)

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode *detectCycle(struct ListNode *head) 
{struct ListNode * slow=head,*fast=head;while(fast && fast->next){slow=slow->next;fast=fast->next->next;if(fast==slow){struct ListNode * meet = slow;while(head!=meet){head=head->next;meet=meet->next;}return meet;}}return NULL;
}

在这里插入图片描述

10. 链表复制

138. 随机链表的复制 - 力扣(LeetCode)

struct Node* copyRandomList(struct Node* head) 
{struct Node* cur=head;while(cur){struct Node* copy=(struct Node*)malloc(sizeof(struct Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = copy->next;}cur = head;while(cur){struct Node* copy = cur->next;if(cur->random == NULL){copy->random = NULL;}else{copy->random = cur->random->next;}cur = copy->next;}struct Node*newhead=NULL,*tail = NULL;cur = head;while(cur){struct Node* copy = cur->next;struct Node* next = copy->next;if(tail == NULL){newhead = tail =copy;}else{tail->next = copy;tail = tail->next;}cur->next = next;cur=next;}return newhead;
}

在这里插入图片描述

11. 输入两个链表,找出它们的第一个公共结点。

160. 相交链表 - 力扣(LeetCode)

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

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

海外媒体发稿:3种媒体宣发套餐内容推广方法

现如今&#xff0c;伴随着信息技术的不断进步和推广&#xff0c;新闻媒体宣发变成企业品牌推广的重要手段之一。为了方便让新闻信息新闻资讯传递给目标群体&#xff0c;公司一般会选择不同的套餐内容和推广方法。下面我们就详细介绍3种新闻资讯新闻媒体宣发套餐内容推广方法。 …

C# 操作 Word 全域查找且替换(含图片对象)

目录 关于全域查找且替换 Word应用样本 SqlServer数据表部分设计样本 范例运行环境 配置Office DCOM 设计实现 组件库引入 实现原理 查找且替换的核心代码 窗格内容 页眉内容 页脚内容 形状内容 小结 关于全域查找且替换 C#全域操作 Word 查找且替换主要包括如下…

【Java】ArrayList数组的扩容机制 jdk1.8

&#x1f4dd;个人主页&#xff1a;哈__ 期待您的关注 ArrayList和普通数组不同&#xff0c;ArrayList支持动态扩容&#xff0c;那么ArrayList到底是如何扩容的呢&#xff1f;你又是否知道ArrayList数组的初始长度是多少呢&#xff1f; 在开始介绍之前&#xff0c;我们要先介…

【Python】如何安装Python

推荐安装python39版本 1.安装python1.1.在任意盘新建Python39目录1.2.双击安装包1.3.安装成功后&#xff0c;WINRcmd进入dos页面&#xff0c;输入 python&#xff0c;即可查看是否安装成功 2.环境变量配置2.1.打开 我的电脑-》高级系统设置-》环境变量-》系统变量2.2.如上图配置…

MHA高可用集群部署

一、MHA的概念 1.1 MHA概述 一套优秀的MySQL高可用环境下故障切换和主从复制的软件MHA的出现就是解决MySQL 单点的问题。 MySQL故障过程中&#xff0c;MHA能做到0-30秒内自动完成故障切换MHA能在故障切换的过程中最大程度上保证数据的一致性以达到真正意义上的高可用 …

​如何用“Dreamina”进行文生图​

文生图&#xff0c;顾名思义&#xff0c;就是用文字描述来生成图片。近年来&#xff0c;随着人工智能技术的进步&#xff0c;文生图技术也逐渐成熟&#xff0c;并逐渐应用于各种领域&#xff0c;例如设计、创作、娱乐等等。 本文将介绍如何使用“Dreamina”进行文生图。 步骤…

2024.3.25-26记:二叉树的遍历

二叉树的遍历深度优先遍历(DFS)递归遍历前序递归遍历&#xff1a;中序递归遍历后续递归遍历 非递归遍历前序非递归遍历中序非递归遍历后续非递归遍历 宽度优先遍历&#xff08;BFS): 二叉树的遍历 二叉树遍历大体上分为深度优先遍历&#xff08;DFS)和宽度优先遍历(BFS)&#…

Linux 常用命令(1)

&#x1f607;作者介绍&#xff1a;一个有梦想、有理想、有目标的&#xff0c;且渴望能够学有所成的追梦人。 &#x1f386;学习格言&#xff1a;不读书的人,思想就会停止。——狄德罗 ⛪️个人主页&#xff1a;进入博主主页 &#x1f5fc;专栏系列&#xff1a;Linux 随笔集合 …

必应bing国内广告推广怎么做,烧不烧钱?

必应Bing作为全球领先的搜索引擎之一&#xff0c;其国内广告平台提供了丰富而精准的广告资源&#xff0c;为众多企业带来极具性价比的品牌推广机会。然而&#xff0c;要想在必应Bing上取得理想的广告效果&#xff0c;并不一定意味着“烧钱”&#xff0c;而是需要通过科学的策略…

Spring日志框架

前言 本文我们简单说说关于Spring中的日志框架,以及对应的注解 我们知道,公司服务器在运行的时候,一定会打印日志,有很多优点,比如预防报警,或者是某重大事故尝试修复等等都需要查看日志 应该说日志对我们来说并不陌生,我们在之前刷题或者是程序遇到bug的时候也经常会将程序的状…

Databricks 开源 DBRX:一款功能强大的新型企业级语言模型

Databricks 公司发布了 DBRX&#xff0c;这是一款性能优异的大语言模型&#xff0c;在各项测试中均超越了现有的开源模型。DBRX 的目标是为企业提供高质量、可定制的 AI 工具&#xff0c;帮助企业更好地利用生成式 AI 技术。 DBRX 的一大亮点是其出色的性能。在语言理解、编程…

Redis 主从复制原理,设计的真巧妙!

前言 今天继续来看看有关 Redis 的一个问题&#xff0c;主从复制。通常&#xff0c;对于大多数的场景来说&#xff0c;读比写更多&#xff0c;于是对于缓存的水平扩展&#xff0c;其中的一个方式 “主从复制” 就是一个常见的思路。有了主从复制&#xff0c;那么可以扩展出很多…

Kibana操作Elasticsearch教程

文章目录 简介ES文档操作创建索引查看索引创建映射字段查看映射关系字段属性详解typeindexstore 字段映射设置流程 新增数据新增会随机生成id新增自定义id智能判断 修改数据删除数据查询基本查询查询所有&#xff08;match_all&#xff09;匹配查询多字段查询词条匹配多词条精确…

大模型预测,下一个token何必是文字?

太快了太快了… 大模型的生成技能&#xff0c;已经到了普通人看不懂的境界&#xff01; 它可以根据用户过去5年的体检报告&#xff0c;生成未来第1年、第2年、第3年的体检报告。 你看&#xff0c;这个生成的过程&#xff0c;是不是像极了ChatGPT&#xff0c;根据历史单词预测…

测开——测试用例设计题

1.测试手机的短信功能需要考虑哪些测试点&#xff1f; 考测试思维 是否能正常打开或进入短信界面短信可以正常编辑、修改、删除短信可以正常发送、接收短信页面的字体、颜色显示是否正常【UI界面 手机设置了字体颜色 大小是否同步】短信的字体是否能够调整同时给多个人发短信…

工业测试测量仪器与人工智能(AI)如何结合

工业测试测量仪器与人工智能&#xff08;AI&#xff09;的结合可以通过多种方式实现&#xff0c;其中一些主要方法包括&#xff1a; 1. 数据分析和预测 智能数据分析&#xff1a;利用AI算法对从传感器和测试仪器收集的数据进行分析&#xff0c;识别模式、趋势和异常&#xff0…

vue+elementUI搭建动态表头的表格

前提&#xff1a;以下代码是vue2项目结合elementUi完成的 数据结构 后端传来的数据是两个list&#xff0c;一个表头的list&#xff0c;一个表格内容的list // 表头 headTableAtts: [{ columnLabel: 姓名, columnName: name },{ columnLabel: 年龄, columnName: age },{ colu…

ensp中pc机访问不同网络的服务器

拓扑图如下&#xff0c;资源已上传 说明&#xff1a;pc通过2个路由访问server服务器 三条线路分别是192.168.1.0网段&#xff0c;192.168.2.0网段和192.168.3.0网段&#xff0c;在未配置的情况下&#xff0c;pc设备是访问不到server的 具体操作流程 第一&#xff1b;pc设备…

简单了解原型模式

什么是原型模式 区别于单例模式&#xff0c;原型模式的一个类可以有多个实例化的对象。 原型模式通过拷贝来产生新的对象&#xff0c;而不是new&#xff0c;并且可以根据自己的需求修改对象的属性。 实现Cloneable接口实现拷贝 而拷贝又分为浅拷贝和深拷贝&#xff0c;两者在…

python的神奇bug2

今天测试出一个很诡异的bug&#xff0c; 这个错误还真的很难发现 测试1 a [1,10,100] for i in a:print(i)if(i10):a[20,30,-1]一般来说我们在进行迭代时&#xff0c;a这个值时不能改动的&#xff0c;但是现在的问题时如果我不小心给改动了呢&#xff0c;结果如下 也就是说…