当前位置:网站首页>【剑指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;
}
};
边栏推荐
- STL value string learning
- USB2.0接口的EMC设计方案
- Unity mouse controls the first person camera perspective
- Unity3d learning note 10 - texture array
- 初探STM32掉电复位PDR
- Leetcode 341. flattened nested list iterator DFS, stack / medium
- 华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
- Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
- Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
- generic paradigm
猜你喜欢

Leetcode 74. search two-dimensional matrix bisection /medium

Spark TroubleShooting整理

Record record record

npm install错误 unable to access

STM32 CAN 通信 滤波设置问题

/dev/loop1占用100%问题

3.3-5v conversion

IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items

generic paradigm

Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
随机推荐
lua学习笔记
QT (five) meta object properties
MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
Deveco studio2.1 operation item error
3D相关的简单数学知识
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
实现自定义Spark优化规则
Record record record
Spark RPC
Leetcode 90. subset II backtracking /medium
Spark3中Catalog组件设计和自定义扩展Catalog实现
华为鸿蒙模拟器去除顶部导航栏方法
EMC design scheme of USB2.0 Interface
EMC design scheme of CAN bus
使用解构交换两个变量的值
Leetcode 74. search two-dimensional matrix bisection /medium
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
STM32 CAN 通信 滤波设置问题
Network equipment hard core technology insider router Chapter 7 tompkinson roaming the network world (Part 2)