当前位置:网站首页>【160. 相交链表】
【160. 相交链表】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
二、题目思路
思路一:暴力求解-穷举
依次取A链表中的每个节点跟B链表中的所有节点比较,如果有地址相同的节点,就是交点
时间复杂度O(N^2)
思路二:
1.尾节点相同就是相交,否则就不相交
2.求交点:长的链表先走(长度差)步,再同时走,第一个相同的就是交点
时间复杂度O(N)
三、题目代码
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode * listA = headA;
struct ListNode * listB = headB;
int lenA=1;int lenB=1;
while(listA->next)
{
listA=listA->next;
lenA++;
}
while(listB->next)
{
listB=listB->next;
lenB++;
}
if(listA != listB)
{
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 shortlist;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- 密码学系列之:PKI的证书格式表示X.509
- Easydss problem and solution summary
- 已解决:一个表中可以有多个自增列吗
- 华为云发布拉美互联网战略
- R language Midwest dataset visualization
- what? You can't be separated by wechat
- UnityEditor 编辑器脚本执行菜单
- 让知识付费系统视频支持M3U8格式播放的方法
- Nestjs integrates config module and Nacos to realize configuration unification
- 【513. 找树左下角的值】
猜你喜欢
mysql8.0忘记密码的详细解决方法

From perceptron to transformer, a brief history of deep learning
![[proteus simulation] 8x8LED dot matrix digital cyclic display](/img/a9/0107eb02c7a081fb466b1c7c12baef.png)
[proteus simulation] 8x8LED dot matrix digital cyclic display

阿里云视频点播播放出错,控制台访问出现code:4400

AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性

EasyClick 固定状态日志窗口

Introduction of neural network (BP) in Intelligent Computing

Resolved: can there be multiple auto incrementing columns in a table

【观察】软件行业创新进入“新周期”,如何在变局中开新局?

R语言midwest数据集可视化
随机推荐
MySQL中如何计算同比和环比
[proteus simulation] H-bridge drive DC motor composed of triode + key forward and reverse control
80-分页查询,不止写法
慕课5、服务发现-Nacos
字节跳动提出轻量级高效新型网络MoCoViT,在分类、检测等CV任务上性能优于GhostNet、MobileNetV3!...
EasyClick 固定状态日志窗口
R语言organdata 数据集可视化
Understand the index of like in MySQL
Ribbon load balancing
Huawei cloud releases Latin American Internet strategy
Visualization of wine datasets in R language
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
深度学习常用损失函数总览:基本形式、原理、特点
It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
阿里云视频点播播放出错,控制台访问出现code:4400
启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
Introduction of neural network (BP) in Intelligent Computing
Kotlin1.6.20新功能Context Receivers使用技巧揭秘
Code to image converter
A Dynamic Near-Optimal Algorithm for Online Linear Programming


