当前位置:网站首页>The first common node of two linked lists -- two questions per day
The first common node of two linked lists -- two questions per day
2022-07-28 07:35:00 【Ink man bookworm】
The finger of the sword Offer 52. The first common node of two linked lists
Brushing questions makes me happy

subject : Enter two linked lists , Find their first common node .
Like the two linked lists below **:**
At the node c1 Start meeting .
Example 1:
Input :intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output :Reference of the node with value = 8
Enter an explanation : The value of the intersection node is 8 ( Be careful , Cannot be if two lists intersect 0). Starting from the respective heads , Linked list A by [4,1,8,4,5], Linked list B by [5,0,1,8,4,5]. stay A in , There is... Before the intersection node 2 Nodes ; stay B in , There is... Before the intersection node 3 Nodes
Ideas
The premise is to understand that the same node means that their node addresses are the same , It's not an element .
Use double pointers to solve , One
The pointerOn A Linked list , OneThe pointerstay B Linked list , The two pointers run separately , Calculate the respective lengths , Long minus broken , Get the difference(gad), Let the long pointer go more first gab Step ,( because 0 To gad The previous nodes must be different , Because we are not elements ), LastTwo pointers go together again, Compare... Together
public class The first common node of two linked lists {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode left=headA;
ListNode right=headB;
int lenA=0;
int lenB=0;
while (left!=null){
lenA++;
left=left.next;
}
while (right!=null){
right=right.next;
lenB++;
}
left=headA;
right=headB;
int gad=0;
ListNode temp=null;
if (lenB>lenA){
temp=left;
left=right;
right=temp;
gad=lenB-lenA;
}else {
gad=lenA-lenB;
}
while (gad>0){
left=left.next;
gad--;
}
while (left!=null){
if (left==right){
return left;
}
left=left.next;
right=right.next;
}
return null;
}
}
边栏推荐
猜你喜欢

两个星期学会软件测试?我震惊了!

如何理解CMS回收器降低gc停顿时间

【google】解决google浏览器不弹出账号密码保存框且无法保存登录信息问题

ESD防护为何对集成电路如此重要?又该如何防护?

xmpp 服务研究(二) prosody 创建账户

(daily question) - the longest substring without repeated characters

“核弹级” Log4j 漏洞仍普遍存在,并造成持续影响

The net loss of users occurred again, and China Mobile, which lost face, launched ultra-low price packages to win users

Guava cache of guava

MHA high availability configuration and failover
随机推荐
Earliest deadline first (EDF)
Basic usage and precautions of arrow function (= >) and three-point operator (...) in ES6 (this points to)
Guava cache of guava
EMC设计攻略 —时钟
(每日一题)——最长不含重复字符的子字符串
[JVM optimization ultra detailed] common JVM tuning scenarios
细说共模干扰和差模干扰
细说共模干扰和差模干扰
高响应比优先
Don't be afraid of ESD static electricity. This article tells you some solutions
【干货】32个EMC标准电路分享!
CAS vs 数据库乐观锁
Introduction to magnetic ring selection and EMC rectification skills
Continous Gesture Recognition with hand-orented spatiotemporal feature
Big talk persistence and redolog
(daily question) - the longest substring without repeated characters
Dry goods | share an EMC actual case and rectification process
What is the root cause of EMC's problems?
4.1.4 why set the member variable to private
EMC之PCB设计技巧

