Problem description
Rt.
Solution Ideas
(1) Both lists are unidirectional linked lists: Determine whether the end nodes of the two lists are the same;
(2) One Ring in two lists, one without ring: impossible to intersect;
(3) Two linked lists have a ring: Slow-fast double pointer method.
Program
public class Listintersection {//Listpublic Boolean isintersectionoftwosinglelist (ListNode L1, ListNode L2) {i F (L1 = = NULL | | l2 = = NULL) {return false;} Whether the end of the list is Samelistnode endOfList1 = getendoflist (L1); ListNode endOfList2 = getendoflist (L2); return endOfList1 = = EndOfList2;} Private ListNode Getendoflist (ListNode head) {if (head = = null) {return null;} ListNode node = head;while (Node.next! = null) {node = Node.next;} return node;} Both list with Cyclepublic Boolean isintersectionoftwolistwithcycle (ListNode L1, ListNode L2) {if (L1 = null | | l2 = = N ULL) {return false;} ListNode slow = L1, fast = L2;while (Fast.next! = NULL | | Fast! = NULL | | Slow! = NULL) {slow = Slow.next;fast = Fast.next . next;if (slow = = fast) {return true;}} return false;}}
Solution 7: Determine if two linked lists Intersect