当前位置:网站首页>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;
}
}
边栏推荐
- [JVM optimization] online JVM tuning practice
- uniapp 移动端 两种横竖屏切换方案
- Big talk persistence and redolog
- Heroku operation summary
- MySQL基础知识学习(二)
- EMC's "don't come back until you rectify"
- Soft exam certificate can be used like this! Get a certificate = get a professional title?
- 磁环选型攻略及EMC整改技巧
- It has been rectified seven times and took half a month. Painful EMC summary
- Which of class A and class B is more stringent in EMC?
猜你喜欢

MHA high availability configuration and failover
![[JVM optimization] online JVM tuning practice](/img/e3/5fa128805af0ca03f0b6715b78d398.jpg)
[JVM optimization] online JVM tuning practice

ThreadLocal things
![[shaders realize negative anti color effect _shader effect Chapter 11]](/img/c5/70761374330eb4fb3915c335b7efb8.png)
[shaders realize negative anti color effect _shader effect Chapter 11]

Retryer of guava

Introduction to magnetic ring selection and EMC rectification skills

Eventbus of guava

CAS vs Database optimistic lock

Dynamic memory management knowledge points

EMC问题的根源在哪?
随机推荐
Safflower STL
Principle and configuration of redis master-slave replication
Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5
Retryer of guava
链表中倒数第k个节点——双指
Guava cache of guava
EMC's "don't come back until you rectify"
【无标题】
Summary of RFID radiation test
Don't be afraid of ESD static electricity. This article tells you some solutions
The net loss of users occurred again, and China Mobile, which lost face, launched ultra-low price packages to win users
通过sed 修改conf文件
Qucs preliminary use guide (not Multism)
EMC rectification method set
RFID辐射测试小结
High performance memory queue -disruptor
Eslint FAQ solutions collection
DNA修饰贵金属纳米颗粒|DNA修饰纳米铜颗粒CuNPS-DNA|研究要点
CAS vs Database optimistic lock
guava之EventBus

