160. Intersection of Two Linked Lists

Question Given the heads of two separately linked lists headA and headB , return the node at which the two lists intersect. If the two linked lists do not intersect at all, return null. For example, the following two linked lists begin to intersect at node c1: Test cases are generated in such a way that there are no loops anywhere in the entire linked structure. Note that linked lists must retain their original structure when the function returns. Own referee: The inputs for the referee are as follows (your program will not receive these inputs): intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersecting node. listA - The first linked list. listB - Second linked list. skipA - The number of nodes to skip forward in list A (starting from the beginning) to reach the intersected node. skipB - The number of nodes to skip forward in list B (starting from the beginning) to reach the intersecting node. The arbiter then creates a linked structure based on these inputs and passes two heads, head A and head B, to your program. If you correctly return the intersected node, your solution is accepted. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 Output: intersected in '8' Explanation: The value of the intersected node is 8 (note that it must not be 0 if the two lists intersect). From head A it reads as [4,1,8,4,5]. From head B it reads as [5,6,1,8,4,5]. There are 2 nodes before the crossed node at A; There are 3 nodes before the intersected node at B. - Note that the intersected node value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (the 3rd node in A and the 4th node in B) point to the same memory location. Example 2: Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 Output: intersected in '2' Explanation: The value of the intersected node is 2 (note that it must not be 0 if the two lists intersect). From head A it reads as [1,9,1,2,4]. From head B it reads as [3,2,4]. There are 3 nodes before the crossed node at A; There is 1 node before the intersected node at B. Example 3: Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 Exit: No intersection Explanation: Head A is read as [2,6,4]. From head B it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be any values. Explanation: The two lists do not intersect, so return null. Solution class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode* a=headA; ListNode* b=headB; while(a!=b){ a=a?a->next:headA; b=b?b->next:headB; } return a; } };

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones