Write a program to find the node at which the intersection of the singly linked lists begins.
For example, the following, linked lists:
A: a1→a2 c1→c2→c3 B: b1→b2→b3
Begin to intersect at node C1.
notes:
- if the and linked lists has no intersection at all, return& Nbsp;
null .
- the linked lists must retain their original structure after the function return S.
- you may assume there is no cycles anywhere in the entire linked Structure.
- your code should preferably run in O (n) time and use only O (1) memory.
The title means finding the intersection point of the two linked list and returning the node, or null if not .
Ideas:1, violence law directly compare with two cycle time complexity of n*m N M is the length of two linked lists respectively2. Hash Table method: Iterates through the list A, and stores the nodes in the Hashtable. Then go through the list B, for each node in B, find the hash table, if found in the Hashtable, it is the intersection of the beginning of the node. Time complexity O (n+m), Spatial complexity O (N) or O (M).
3, double pointer method: Define two pointers, respectively, to the Heada and headb, respectively, to find out two of the number of chain nodes (length), and then Temp=abs (NUMA-NUMB) to find the difference between the two, let the Long Chain (Max (NUMA,NUMB) first go to the temp step, Then traverse backwards from the current position of the long chain and the head of the short chain, knowing to find the sameas above
A: a1→a2 c1→c2→c3 B: b1→b2→b3
The long chain is B, so B go first abs (6-5) = 1 The current position of the step chain is B2, then a A1 two pointer from the back.
a method of deformation for two-pointer method:The above needs to traverse a B first, if the first traversal to let A and B go together, there is a null to stopso at this time the short chain has gone, long chain has gone short chain length, the remaining length is two chain length difference
as above
A: a1→a2 c1→c2→c3 B: b1→b2→b3
at this point, the current position of B is C3 and then a pointer to a B1 (the head is not finished) the current position and the pointer to a at the same time to go back, the current position points to null end, pointing to a pointer at this point of B2, that is, the top of the first step of the length of the path, and then Follow the rest and find the intersection .
The code is as follows:
<span style= "FONT-SIZE:18PX;" >/** * 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 (HeadA==NU ll| | Headb==null) return NULL; ListNode *p,*q; P=heada; Q=HEADB; int numa=0; Defines the number of chain nodes that NumA uses to record Heada int numb=0;//defines the number of link nodes that NumB use to record headb while (p) {++numa; p=p->next; } while (q) {++numb; q=q->next; } P=heada; Q=HEADB; int Temp=abs (NUMA-NUMB); if (NUMA>NUMB)//Determine which chain length, long that chain first go |numa-numb| Step {while (temp>0) {p=p->next; --temp; }} else {while (temp>0) {q=q->next; --temp; }} while (P&&Q)//Long Walk, both walk together until an overlapping node {if (p==q) return p is encountered; p=p->next; q=q->next; } return NULL; }};</span>
Intersection of Linked Lists Leetcode