当前位置:网站首页>LeetCode链表问题——面试题02.07.链表相交(一题一文学会链表)
LeetCode链表问题——面试题02.07.链表相交(一题一文学会链表)
2022-07-28 19:46:00 【十八岁讨厌Java】
一、题目描述:
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
图示两个链表在节点 c1 开始相交:

二、代码
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}三、了解链表
什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链接的入口节点称为链表的头结点也就是head。
单链表:
双链表:
每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
双链表 既可以向前查询也可以向后查询。

循环链表:
循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。

链表的存储方式:
数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
删除节点:
只要将C节点的next指针 指向E节点就可以了。

添加节点:

与数组对比:

边栏推荐
- 苹果M1处理器详解:性能及能效成倍提升,Intel酷睿i9也不是对手!
- 一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
- C # detailed steps for connecting to MySQL database
- 基于Xilinx的时序分析与约束
- How NPM switches Taobao source images
- Eureka registers with each other, only showing each other or only showing problems in one
- C process control statement
- 八、QOS队列调度与报文丢弃
- ABB电磁流量计维修信号变送器维修41F/E4技术参数
- MySQL
猜你喜欢

BUUCTF做题Upload-Labs记录pass-01~pass-10

Summary of 29 typical problems in Devops planning and construction of securities enterprises based on containerized PAAS platform

How to measure software architecture

Redis缓存雪崩、缓存穿透、缓存击穿

Nacos principle

Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector

Attribute based encryption simulation and code implementation (cp-abe) paper: ciphertext policy attribute based encryption

SQL Server 数据库之备份和恢复数据库

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

Ctfshow network lost track record (2)
随机推荐
System integration under microservice architecture
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
BUUCTF做题Upload-Labs记录pass-01~pass-10
Guanghetong & Qualcomm Internet of things technology open day successfully held
High salary in the workplace | "intermediate and advanced test" interview questions
国产芯片厂商助力,2020年白牌TWS耳机出货已达6亿部
学习Typescript(二)
1162. 地图分析-非递归法
【Bluetooth蓝牙开发】八、BLE协议之传输层
1162. Map analysis - non recursive method
Automatic filling of spare parts at mobile end
工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
小程序容器技术,让移动研发效率提升500%
[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)
微星宝安工厂失火!官方回应:无人员受伤,产线不受影响!
MySQL
一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
Kubeadm搭建kubernetes集群
承载银行关键应用的容器云平台如何选型及建设?