当前位置:网站首页>【剑指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;
}
};
边栏推荐
- Distributed lock
- Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
- Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
- Four kinds of relay schemes driven by single chip microcomputer
- /dev/loop1占用100%问题
- Network equipment hard core technology insider router 20 dpdk (V)
- AssetBundle如何打包
- Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
- Network equipment hard core technology insider router Chapter 5 tompkinson roaming the network world (Part 1)
- 华为鸿蒙模拟器去除顶部导航栏方法
猜你喜欢

谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues

STL value string learning

西瓜书《机器学习》阅读笔记之第一章绪论

Several basic uses of tl431-2.5v voltage reference chip

华为鸿蒙模拟器去除顶部导航栏方法

Record record record

Leetcode 783. binary search tree node minimum distance tree /easy

Digital storage oscilloscope based on FIFO idt7202-12

学习Parquet文件格式

Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
随机推荐
Discussion on STM32 power down reset PDR
Record record record
Leetcode 1143. dynamic programming of the longest common subsequence /medium
Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
复杂度分析
STM32 can communication filter setting problem
Unity mouse controls the first person camera perspective
后台返回来的是这种数据,是什么格式啊
With just two modifications, apple gave styleganv2 3D generation capabilities
Unity3d learning note 10 - texture array
《剑指Offer》 链表反转
Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化
Leetcode 190. reverse binary bit operation /easy
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key
Simple mathematical knowledge related to 3D
EMC design scheme of CAN bus
Watermelon book machine learning reading notes Chapter 1 Introduction
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
How to take satisfactory photos / videos from hololens