当前位置:网站首页>【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
2022-07-27 14:24:00 【Jocelin47】
方法一:哈希表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map< ListNode*, int > record;
if( headA == nullptr || headB == nullptr)
return nullptr;
while( headA != nullptr )
{
record[headA]++;
headA = headA->next;
}
while( headB != nullptr )
{
if( ( ++record[headB] ) >= 2)
return headB;
headB = headB->next;
}
return nullptr;
}
};
方法二:双指针
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *L1=headA;
ListNode *L2=headB;
if( headB == nullptr || headA == nullptr)
return NULL;
while( L1 != L2 )
{
L1 = L1 != NULL ? L1->next : headB;
L2 = L2 != NULL ? L2->next : headA;
}
return L1;
}
};
方法三:栈实现
将所有的节点地址的数据推入到栈中,再后进先出
比较后面相同的数据,如果遇到了不同的数据则把上一次相同的最后一个节点返回就好了。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if( headB == nullptr || headA == nullptr)
return NULL;
stack< ListNode* > stack_L1;
stack< ListNode* > stack_L2;
ListNode* tempL1 = nullptr;
ListNode* tempL2 = nullptr;
while( headA != nullptr )
{
stack_L1.push(headA);
headA = headA->next;
}
while( headB != nullptr )
{
stack_L2.push(headB);
headB = headB->next;
}
ListNode* result = nullptr;
while( !stack_L1.empty() && !stack_L2.empty() )
{
if( !stack_L1.empty() )
tempL1 = stack_L1.top();
if( !stack_L2.empty() )
tempL2 = stack_L2.top();
if( tempL1 != tempL2 )
break;
result = stack_L1.top();
if( !stack_L1.empty() ) stack_L1.pop();
if( !stack_L2.empty() ) stack_L2.pop();
}
return result;
}
};
边栏推荐
- 3.3-5v转换
- Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
- Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard
- 华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
- Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
- Leetcode 81. search rotation sort array II binary /medium
- DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
- After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
- How to package AssetBundle
- Unity3d learning note 10 - texture array
猜你喜欢

4种单片机驱动继电器方案

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制

Pictures to be delivered

3.3-5v conversion

TL431-2.5v基准电压芯片几种基本用法

MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face

Leetcode-1737-满足三条件之一需改变的最少字符数

Spark 任务Task调度异常分析

Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022

Basic usage of kotlin
随机推荐
Unity3d learning note 10 - texture array
Method of removing top navigation bar in Huawei Hongmeng simulator
学习Parquet文件格式
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
Is it safe to open an account on a mobile phone?
Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
Spark Filter算子在Parquet文件上的下推
设置提示框位置随鼠标移动,并解决提示框显示不全的问题
Leetcode 244周赛-赛后补题题解【西兰花选手】
HJ8 合并表记录
STM32 can -- can ID filter analysis
DevEco Studio2.1运行项目报错
md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
Lua study notes
STM32 can communication filter setting problem
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
Spark 3.0 测试与使用
Spark动态资源分配的资源释放过程及BlockManager清理过程
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)