Method a: violence, traverse the linked list of each node A, the list B to be checked O (AB)
Method Two: Since the title list is unidirectional, so that after the first point of coincidence, the other are equal, that is Y-shaped. So they tail nodes are the same, and therefore weight behind the start line with "last-out characteristics", we can define two stacks, then the list into the stack, the stack during the comparison;
Method three: first get two lists of length, if the length of the longer length shorter than m, the step length m and then go down together and start comparing
Findfrist ListNode (ListNode A, B ListNode) { // Get the length of the list int Alength =. 1, bLength =. 1 ; ListNode A1 = A; the while (! A1.next = null ) { A1 = A1.next; Alength ++ ; } Bl ListNode = B; the while (B1.next =! null ) { Bl = B1.next; bLength ++ ; } // long go k steps int K = B.length- A.length; the while (K <0 ) { K ++ ; A = A.next; } the while (K> 0 ) { K - ; B = B.next; } // equivalent Comparative started until it finds the intersection of the direct return length · the while (Alength> 0 ) { IF (A == B) return A; A = A.next; B = B.next; A.length- ; } // no intersection in the end is null return null ; }
Note: If the node as the root of the tail, then two trees looking like a common ancestor