0160. Intersection of Two Linked Lists - chasel2361/leetcode GitHub Wiki
💡 Given the heads of two singly linked-lists headA
and headB
, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null
.
For example, the following two linked lists begin to intersect at node c1
:
💡 The test cases are generated such that there are no cycles anywhere in the entire linked structure. Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
-
intersectVal
- The value of the node where the intersection occurs. This is0
if there is no intersected node. -
listA
- The first linked list. -
listB
- The second linked list. -
skipA
- The number of nodes to skip ahead inlistA
(starting from the head) to get to the intersected node. -
skipB
- The number of nodes to skip ahead inlistB
(starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads, headA
and headB
to your program. If you correctly return the intersected node, then your solution will be 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 at '8' Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
💡 **Example 2:**Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 Output: Intersected at '2' Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
💡 Example 3:
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 Output: No intersection Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. Explanation: The two lists do not intersect, so return null.
這題的重點在於要找到交會的節點,如果在交會前出現相同的節點值也不能算數。
看了提示才想到應該要先把兩個linked list走一遍抓長度,先讓兩條剩餘的長度相等,在一步一步比next的指標是否相同,這樣就可以找到交會的節點了。程式碼如下:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
get_length = self.get_length
move_eq = self.move_to_equal
ptr_A, ptr_B = headA, headB
len_A = get_length(headA)
len_B = get_length(headB)
if len_A > len_B:
ptr_A = move_eq(ptr_A, len_A-len_B)
else:
ptr_B = move_eq(ptr_B, len_B-len_A)
while ptr_A is not None:
if ptr_A == ptr_B:
return ptr_A
ptr_A = ptr_A.next
ptr_B = ptr_B.next
return None
def move_to_equal(self, head, steps):
ptr = head
for _ in range(steps):
ptr = ptr.next
return ptr
def get_length(self, head):
length = 0
ptr = head
while ptr is not None:
length += 1
ptr = ptr.next
return length
這樣寫的時間複雜度為O(N) (list最多要走兩遍),空間複雜度為 O(1)
概念一樣,寫起來長這樣
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB){
int len_A = size(headA);
int len_B = size(headB);
int diff = len_A - len_B;
if(diff > 0){
while(diff > 0){
headA = headA->next;
diff--;
}
}
else{
while (diff < 0){
headB = headB->next;
diff++;
}
}
while(headA){
if(headA == headB) return headA;
headA = headA->next;
headB = headB->next;
}
return NULL;
}
int size(ListNode* node){
if(node == NULL) return 0;
return size(node->next) + 1;
}
};