【LeetCode & 劍指offer刷題】鏈表題5:52 兩個鏈表的第一個公共結點(Intersection of Two Linked Lists)
阿新 • • 發佈:2019-01-05
http edi || test lee tint lists font ase * pHead1, ListNode* pHead2)
{
//求兩鏈表的長度
int len1 = findListLength(pHead1);
int len2 = findListLength(pHead2);
ListNode* plong = pHead1, *pshort = pHead2;
if(len1 < len2)
{
pshort = pHead1;
plong = pHead2;
}
for(int i = 1; i <= abs(len1-len2); i++) plong = plong->next; //較長的鏈表多走幾步
//同時步進,直到遇到相同結點或者均遇到尾結點
while(plong != pshort)
{
plong = plong->next;
pshort = pshort->next;
}
return plong;
}
private:
int findListLength (ListNode* p)
{
int n = 0;
while(p != nullptr)
{
p = p->next;
n++;
}
return n;
}
};
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:
//問題:求兩鏈表的交匯點 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /* 方法:雙指針法 (1) 如果有交匯點,p1掃描A,p2掃描B,掃描到結尾後,p1重定向到headB,p2重定向到headA,之後一定會在交匯點處相遇 因為交匯點之後都是路徑相同的,交匯點之前的路徑差可以由互換的兩次掃描中抵消 (2) 如果沒有交匯點,p1最後會到b末尾,p2會到a末尾,p1=p2=null,退出程序 O(m+n),O(1) */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *p1 = headA; ListNode *p2 = headB; if(p1 == NULL || p2 == NULL) return NULL;
while(p1 && p2 && p1!=p2) //只要不為空,進行掃描(註意,加上p1!=p2的判斷,可能兩鏈表長度為1,且相交,不加的話會返回null) { p1 = p1->next; p2 = p2->next; if(p1 == p2) return p1; //p1,p2同時為nullptr或者指向交匯點 if(p1 == NULL) p1 = headB; //重定向到另一個鏈表首結點 if(p2 == NULL) p2 = headA; } return p1; } };
【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...)
52 兩個鏈表的第一個公共結點
題目描述
輸入兩個鏈表,找出它們的第一個公共結點。 /* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ #include <cmath> class Solution { public: ListNode* FindFirstCommonNode( ListNode- 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.
//問題:求兩鏈表的交匯點 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /* 方法:雙指針法 (1) 如果有交匯點,p1掃描A,p2掃描B,掃描到結尾後,p1重定向到headB,p2重定向到headA,之後一定會在交匯點處相遇 因為交匯點之後都是路徑相同的,交匯點之前的路徑差可以由互換的兩次掃描中抵消 (2) 如果沒有交匯點,p1最後會到b末尾,p2會到a末尾,p1=p2=null,退出程序 O(m+n),O(1) */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *p1 = headA; ListNode *p2 = headB; if(p1 == NULL || p2 == NULL) return NULL;
while(p1 && p2 && p1!=p2) //只要不為空,進行掃描(註意,加上p1!=p2的判斷,可能兩鏈表長度為1,且相交,不加的話會返回null) { p1 = p1->next; p2 = p2->next; if(p1 == p2) return p1; //p1,p2同時為nullptr或者指向交匯點 if(p1 == NULL) p1 = headB; //重定向到另一個鏈表首結點 if(p2 == NULL) p2 = headA; } return p1; } };
【LeetCode & 劍指offer刷題】鏈表題5:52 兩個鏈表的第一個公共結點(Intersection of Two Linked Lists)