[leetCode][003] Intersection of Two Linked Lists

【题目】:  

  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:
    1. If the two linked lists have no intersection at all, return null.
    2. The linked lists must retain their original structure after the function returns.
    3. You may assume there are no cycles anywhere in the entire linked structure.
    4. Your code should preferably run in O(n) time and use only O(1) memory

【题意解析】:

  输入两个单链表,判断这两个单链表是否相交,返回相交点。这里有几种方法供大家参考。

 

【解法1】

  《编程之美》一书中有讲到该问题,如何判断这两个链表相交,可以讲其中一个链表的头尾相连,然后从另一个链表的角度来判断时候有环状链表存在即可。相当于将问题转换成如何求有环单链表的环状入口点?详细方案见我的另一篇专门讲环状单链表的文章 《关于有环单链表的那些事儿》。需要注意的是,不能改变原有链表的结构,因此方法返回的时候需要恢复原状。

  (1)将链表B的尾节点指向其头节点HeadB,因此链表B形成环状,链表A成为有环单链表;

  (2)此时问题转换成求链表A上环状入口点;

  (3)恢复链表B的结构;

  代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         if (nullptr == headA || nullptr == headB){
13             return nullptr;
14         }
15 
16         ListNode *pNode = headB;
17         while (pNode->next){
18             pNode = pNode->next;
19         }
20         ListNode *pRear = pNode;
21         pNode->next = headB;
22 
23         ListNode *pJoint = loopJoint(headA);
24         pRear->next = nullptr;   // 恢复
25 
26         return pJoint;
27     }
28     
29 private:    
30     ListNode *loopJoint(ListNode *pHead){
31         if (nullptr == pHead || nullptr == pHead->next){
32             return nullptr;
33         }
34 
35         ListNode *pSlow = pHead;
36         ListNode *pFast = pHead;
37         while (pFast && pFast->next){
38             pFast = pFast->next->next;
39             pSlow = pSlow->next;
40 
41             if (pFast == pSlow){
42                 break;
43             }
44         }
45 
46         if (nullptr == pFast || nullptr == pFast->next){
47             return nullptr;
48         }
49 
50         pSlow = pHead;
51         while (pFast != pSlow){
52             pFast = pFast->next;
53             pSlow = pSlow->next;
54         }
55 
56         return pSlow;
57     }
58 };

 

【解法2】 

  《剑指Offer》一书中提到的这个方法,如果两个没有环的链表相交于某个节点,那么在这个节点之后的所有节点都是两个链表所共有的。因此两个链表从交织点之前的部分长度差即为整条链表的长度差,我们只要让两条链表从离交织点相同距离的位置开始前进,经过O(n)步,一定能够找到交织点。

  (1)遍历链表A,记录其长度len1,遍历链表B,记录其长度len2。

  (2)按尾部对齐,如果两个链表的长度不相同,让长度更长的那个链表从头节点先遍历abs(len1-en2),这样两个链表指针指向对齐的位置。

  (3)然后两个链表齐头并进,当它们相等时,就是交集的节点。

  时间复杂度O(n+m),空间复杂度O(1)

  代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         if (nullptr == headA || nullptr == headB){
13             return nullptr;
14         }
15 
16         int nLongLength = 0;
17         int nShortLength = 0;
18         ListNode *pLongList = headA;
19         ListNode *pShortList = headB;
20 
21         while (pLongList){
22             ++nLongLength;
23             pLongList = pLongList->next;
24         }
25 
26         while (pShortList){
27             ++nShortLength;
28             pShortList = pShortList->next;
29         }
30 
31         if (nShortLength > nLongLength){
32             pLongList = headB;
33             pShortList = headA;
34 
35             // 校正
36             nLongLength ^= nShortLength;
37             nShortLength ^= nLongLength;
38             nLongLength ^= nShortLength;
39         }
40         else{
41             pLongList = headA;
42             pShortList = headB;
43         }
44 
45        // int offset = (nLongLength - nShortLength > 0) ? (nLongLength - nShortLength) : (nShortLength - nLongLength);
46         int offset = nLongLength - nShortLength;
47         while (offset> 0){
48             pLongList = pLongList->next;
49             --offset;
50         }
51 
52         while (pLongList != pShortList){
53             pLongList = pLongList->next;
54             pShortList = pShortList->next;
55         }
56 
57         return pLongList;
58     }
59 };

【解法3】

  还有一种解法,实际上还是利用步差来实现的,具体证明还米有搞懂,思考中。具体如下:

    (1)维护两个指针pA和pB,初始分别指向链表A和链表B。然后让它们分别遍历整个链表,每步一个节点。

    (2)当pA到达链表末尾时,让它指向B的头节点;类似的当pB到达链表末尾时,重新指向A的头节点。

    (3)当pA和pB相遇时也即为交织点。

  该算法的时间复杂度依然为O(m + n),时间复杂度为O(1)

  代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12         if (nullptr == headA || nullptr == headB){
13             return nullptr;
14         }
15 
16         ListNode *nodeA = headA;
17         ListNode *nodeB = headB;
18         while (nodeA && nodeB && nodeA != nodeB){
19             nodeA = nodeA->next;
20             nodeB = nodeB->next;
21 
22             if (nodeA == nodeB){
23                 break;
24             }
25 
26             if (nullptr == nodeA){
27                 nodeA = headB;
28             }
29 
30             if (nullptr == nodeB){
31                 nodeB = headA;
32             }
33         }
34 
35         return nodeA;
36     }
37 };

  如果错误,希望各位不吝赐教,谢谢

 

posted @ 2015-02-04 12:25  菜鸟加贝的爬升  阅读(434)  评论(0编辑  收藏  举报