Intersection of Two Linked Lists
1、题目
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return
null
. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
2、分析
题目大意:给两个链表,找出它们交集的那个节点,要求时间复杂度O(n),空间复杂度O(1)。
有以下几种思路:
(1) 暴力破解 ,遍历链表A的所有节点,并且对于每个节点,都与链表B中的所有节点比较,退出条件是在B中找到第一个相等的节点。时间复杂度O(lengthA*lengthB),空间复杂度O(1)。
(2) 哈希表 。遍历链表A,并且将节点存储到哈希表中。接着遍历链表B,对于B中的每个节点,查找哈希表,如果在哈希表中找到了,说明是交集开始的那个节点。时间复杂度O(lengthA+lengthB),空间复杂度O(lengthA)或O(lengthB)。
(3) 双指针法 ,指针pa、pb分别指向链表A和B的首节点。
遍历链表A,记录其长度lengthA,遍历链表B,记录其长度lengthB。
因为两个链表的长度可能不相同,比如题目所给的case,lengthA=5,lengthB=6,则作差得到 lengthB- lengthA=1,将指针pb从链表B的首节点开始走1步,即指向了第二个节点,pa指向链表A首节点,然后它们同时走,每次都走一步,当它们相等时,就是交集的节点。
时间复杂度O(lengthA+lengthB),空间复杂度O(1)。双指针法的代码如下:
3、代码
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *pa=headA,*pb=headB; int lengthA=0,lengthB=0; while(pa) {pa=pa->next;lengthA++;} while(pb) {pb=pb->next;lengthB++;} if(lengthA<=lengthB){ int n=lengthB-lengthA; pa=headA;pb=headB; while(n) {pb=pb->next;n--;} }else{ int n=lengthA-lengthB; pa=headA;pb=headB; while(n) {pa=pa->next;n--;} } while(pa!=pb){ pa=pa->next; pb=pb->next; } return pa; }
4、题目作者的解答(英文)作者双指针法花费的时间比我的方法更少
There are many solutions to this problem:
- Brute-force solution (O(mn) running time, O(1) memory):
For each node a i in list A, traverse the entire list B and check if any node in list B coincides with a i .
- Hashset solution (O(n+m) running time, O(n) or O(m) memory):
Traverse list A and store the address / reference to each node in a hash set. Then check every node b i in list B: if b i appears in the hash set, then b i is the intersection node.
- Two pointer solution (O(n+m) running time, O(1) memory):
- Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
- When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
- If at any point pA meets pB, then pA/pB is the intersection node.
- To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
- If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.